mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-14 23:07:56 +02:00
Feature net udp (#2347)
* add PMTU discovery #2329 * add socket gather/scatter capabilities #2330 (win, udp) * enable WSAPoll * add FastMemoryPool * add receiveFrom() with native args * allow copying of StringTokenizer * add AtomicFlag and SpinlockMutex * update .gitignore * UDPServer and client #2343 (windows) * fix warnings * fix warnings * regenerate Net VS solutions * regenerate CppUnit projects/solutions * clang fixes * gcc fixes * try to fix travis * more travis fixes * more travis fixes * handle UDPClient exception * fix makefiles and init order warnings * add UNIX gather/scatter sendto/recvfrom implementations and tests * run travis tests as sudo * try to run tests as sudo, 2nd attempt * fix warning * use mutex in reactor * lock-order-inversion in SocketReactor #2346 * add PMTU discovery #2329 (linux) * ICMPSocket does not check reply address #1921 * remove some ignored tests * add PMTU discovery #2329 (reconcile logic with #1921) * fix native receiveFrome() * reinstate ignoring of proxy errors * add testMTU to ignore list * add include atomic * NTPClient not checking reply address #2348 * some ICMP/MTU fixes * UDPSocketReader cleanup * resolve some socket inheritance warnings * add NTP time sync to ignored tests * SocketNotifier not thread-safe #2345 * prevent x64 samples build attempt for win32 * build TestApp and Library * fix ICMP tests * regen VS projects * regen VS projects and add missing 2012 files * remove debug prints
This commit is contained in:

committed by
GitHub

parent
da15142f69
commit
c4e676d36d
@@ -21,6 +21,17 @@
|
||||
#include "Poco/Foundation.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "Poco/ScopedLock.h"
|
||||
#include "Poco/Timestamp.h"
|
||||
|
||||
#if __cplusplus >= 201103L
|
||||
#ifndef POCO_HAVE_STD_ATOMICS
|
||||
#define POCO_HAVE_STD_ATOMICS
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef POCO_HAVE_STD_ATOMICS
|
||||
#include <atomic>
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(POCO_OS_FAMILY_WINDOWS)
|
||||
@@ -150,6 +161,58 @@ private:
|
||||
};
|
||||
|
||||
|
||||
#ifdef POCO_HAVE_STD_ATOMICS
|
||||
|
||||
class Foundation_API SpinlockMutex
|
||||
/// A SpinlockMutex, implemented in terms of std::atomic_flag, as
|
||||
/// busy-wait mutual exclusion.
|
||||
///
|
||||
/// While in some cases (eg. locking small blocks of code)
|
||||
/// busy-waiting may be an optimal solution, in many scenarios
|
||||
/// spinlock may not be the right choice - it is up to the user to
|
||||
/// choose the proper mutex type for their particular case.
|
||||
///
|
||||
/// Works with the ScopedLock class.
|
||||
{
|
||||
public:
|
||||
typedef Poco::ScopedLock<SpinlockMutex> ScopedLock;
|
||||
|
||||
SpinlockMutex();
|
||||
/// Creates the SpinlockMutex.
|
||||
|
||||
~SpinlockMutex();
|
||||
/// Destroys the SpinlockMutex.
|
||||
|
||||
void lock();
|
||||
/// Locks the mutex. Blocks if the mutex
|
||||
/// is held by another thread.
|
||||
|
||||
void lock(long milliseconds);
|
||||
/// Locks the mutex. Blocks up to the given number of milliseconds
|
||||
/// if the mutex is held by another thread. Throws a TimeoutException
|
||||
/// if the mutex can not be locked within the given timeout.
|
||||
|
||||
bool tryLock();
|
||||
/// Tries to lock the mutex. Returns immediately, false
|
||||
/// if the mutex is already held by another thread, true
|
||||
/// if the mutex was successfully locked.
|
||||
|
||||
bool tryLock(long milliseconds);
|
||||
/// Locks the mutex. Blocks up to the given number of milliseconds
|
||||
/// if the mutex is held by another thread.
|
||||
/// Returns true if the mutex was successfully locked.
|
||||
|
||||
void unlock();
|
||||
/// Unlocks the mutex so that it can be acquired by
|
||||
/// other threads.
|
||||
|
||||
private:
|
||||
std::atomic_flag _flag = ATOMIC_FLAG_INIT;
|
||||
};
|
||||
|
||||
#endif // POCO_HAVE_STD_ATOMICS
|
||||
|
||||
|
||||
class Foundation_API NullMutex
|
||||
/// A NullMutex is an empty mutex implementation
|
||||
/// which performs no locking at all. Useful in policy driven design
|
||||
@@ -202,6 +265,11 @@ public:
|
||||
//
|
||||
// inlines
|
||||
//
|
||||
|
||||
//
|
||||
// Mutex
|
||||
//
|
||||
|
||||
inline void Mutex::lock()
|
||||
{
|
||||
lockImpl();
|
||||
@@ -233,6 +301,10 @@ inline void Mutex::unlock()
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// FastMutex
|
||||
//
|
||||
|
||||
inline void FastMutex::lock()
|
||||
{
|
||||
lockImpl();
|
||||
@@ -264,6 +336,55 @@ inline void FastMutex::unlock()
|
||||
}
|
||||
|
||||
|
||||
#ifdef POCO_HAVE_STD_ATOMICS
|
||||
|
||||
//
|
||||
// SpinlockMutex
|
||||
//
|
||||
|
||||
inline void SpinlockMutex::lock()
|
||||
{
|
||||
while (_flag.test_and_set(std::memory_order_acquire));
|
||||
}
|
||||
|
||||
|
||||
inline void SpinlockMutex::lock(long milliseconds)
|
||||
{
|
||||
Timestamp now;
|
||||
Timestamp::TimeDiff diff(Timestamp::TimeDiff(milliseconds)*1000);
|
||||
while (_flag.test_and_set(std::memory_order_acquire))
|
||||
{
|
||||
if (now.isElapsed(diff)) throw TimeoutException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline bool SpinlockMutex::tryLock()
|
||||
{
|
||||
return !_flag.test_and_set(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
|
||||
inline bool SpinlockMutex::tryLock(long milliseconds)
|
||||
{
|
||||
Timestamp now;
|
||||
Timestamp::TimeDiff diff(Timestamp::TimeDiff(milliseconds)*1000);
|
||||
while (_flag.test_and_set(std::memory_order_acquire))
|
||||
{
|
||||
if (now.isElapsed(diff)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
inline void SpinlockMutex::unlock()
|
||||
{
|
||||
_flag.clear(std::memory_order_release);
|
||||
}
|
||||
|
||||
#endif // POCO_HAVE_STD_ATOMICS
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user