⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lock.h

📁 开源备份软件源码 AMANDA, the Advanced Maryland Automatic Network Disk Archiver, is a backup system that a
💻 H
📖 第 1 页 / 共 3 页
字号:
    if (thread_in_use () && rw_rdlock (&NAME) != 0) abort ()# define gl_rwlock_wrlock(NAME) \    if (thread_in_use () && rw_wrlock (&NAME) != 0) abort ()# define gl_rwlock_unlock(NAME) \    if (thread_in_use () && rw_unlock (&NAME) != 0) abort ()# define gl_rwlock_destroy(NAME) \    if (thread_in_use () && rwlock_destroy (&NAME) != 0) abort ()/* --------------------- gl_recursive_lock_t datatype --------------------- *//* Old Solaris threads did not have recursive locks.   We have to implement them ourselves.  */typedef struct        {          mutex_t mutex;          thread_t owner;          unsigned long depth;        }        gl_recursive_lock_t;# define gl_recursive_lock_define(STORAGECLASS, NAME) \    STORAGECLASS gl_recursive_lock_t NAME;# define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \    STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;# define gl_recursive_lock_initializer \    { DEFAULTMUTEX, (thread_t) 0, 0 }# define gl_recursive_lock_init(NAME) \    if (thread_in_use ()) glthread_recursive_lock_init (&NAME)# define gl_recursive_lock_lock(NAME) \    if (thread_in_use ()) glthread_recursive_lock_lock (&NAME)# define gl_recursive_lock_unlock(NAME) \    if (thread_in_use ()) glthread_recursive_lock_unlock (&NAME)# define gl_recursive_lock_destroy(NAME) \    if (thread_in_use ()) glthread_recursive_lock_destroy (&NAME)extern void glthread_recursive_lock_init (gl_recursive_lock_t *lock);extern void glthread_recursive_lock_lock (gl_recursive_lock_t *lock);extern void glthread_recursive_lock_unlock (gl_recursive_lock_t *lock);extern void glthread_recursive_lock_destroy (gl_recursive_lock_t *lock);/* -------------------------- gl_once_t datatype -------------------------- */typedef struct        {          volatile int inited;          mutex_t mutex;        }        gl_once_t;# define gl_once_define(STORAGECLASS, NAME) \    STORAGECLASS gl_once_t NAME = { 0, DEFAULTMUTEX };# define gl_once(NAME, INITFUNCTION) \    do                                                \      {                                               \        if (thread_in_use ())                         \          {                                           \            glthread_once (&NAME, INITFUNCTION);      \          }                                           \        else                                          \          {                                           \            if (glthread_once_singlethreaded (&NAME)) \              INITFUNCTION ();                        \          }                                           \      }                                               \    while (0)extern void glthread_once (gl_once_t *once_control, void (*initfunction) (void));extern int glthread_once_singlethreaded (gl_once_t *once_control);# ifdef __cplusplus}# endif#endif/* ========================================================================= */#if USE_WIN32_THREADS# include <windows.h># ifdef __cplusplusextern "C" {# endif/* We can use CRITICAL_SECTION directly, rather than the Win32 Event, Mutex,   Semaphore types, because     - we need only to synchronize inside a single process (address space),       not inter-process locking,     - we don't need to support trylock operations.  (TryEnterCriticalSection       does not work on Windows 95/98/ME.  Packages that need trylock usually       define their own mutex type.)  *//* There is no way to statically initialize a CRITICAL_SECTION.  It needs   to be done lazily, once only.  For this we need spinlocks.  */typedef struct { volatile int done; volatile long started; } gl_spinlock_t;/* -------------------------- gl_lock_t datatype -------------------------- */typedef struct        {          gl_spinlock_t guard; /* protects the initialization */          CRITICAL_SECTION lock;        }        gl_lock_t;# define gl_lock_define(STORAGECLASS, NAME) \    STORAGECLASS gl_lock_t NAME;# define gl_lock_define_initialized(STORAGECLASS, NAME) \    STORAGECLASS gl_lock_t NAME = gl_lock_initializer;# define gl_lock_initializer \    { { 0, -1 } }# define gl_lock_init(NAME) \    glthread_lock_init (&NAME)# define gl_lock_lock(NAME) \    glthread_lock_lock (&NAME)# define gl_lock_unlock(NAME) \    glthread_lock_unlock (&NAME)# define gl_lock_destroy(NAME) \    glthread_lock_destroy (&NAME)extern void glthread_lock_init (gl_lock_t *lock);extern void glthread_lock_lock (gl_lock_t *lock);extern void glthread_lock_unlock (gl_lock_t *lock);extern void glthread_lock_destroy (gl_lock_t *lock);/* ------------------------- gl_rwlock_t datatype ------------------------- *//* It is impossible to implement read-write locks using plain locks, without   introducing an extra thread dedicated to managing read-write locks.   Therefore here we need to use the low-level Event type.  */typedef struct        {          HANDLE *array; /* array of waiting threads, each represented by an event */          unsigned int count; /* number of waiting threads */          unsigned int alloc; /* length of allocated array */          unsigned int offset; /* index of first waiting thread in array */        }        gl_waitqueue_t;typedef struct        {          gl_spinlock_t guard; /* protects the initialization */          CRITICAL_SECTION lock; /* protects the remaining fields */          gl_waitqueue_t waiting_readers; /* waiting readers */          gl_waitqueue_t waiting_writers; /* waiting writers */          int runcount; /* number of readers running, or -1 when a writer runs */        }        gl_rwlock_t;# define gl_rwlock_define(STORAGECLASS, NAME) \    STORAGECLASS gl_rwlock_t NAME;# define gl_rwlock_define_initialized(STORAGECLASS, NAME) \    STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;# define gl_rwlock_initializer \    { { 0, -1 } }# define gl_rwlock_init(NAME) \    glthread_rwlock_init (&NAME)# define gl_rwlock_rdlock(NAME) \    glthread_rwlock_rdlock (&NAME)# define gl_rwlock_wrlock(NAME) \    glthread_rwlock_wrlock (&NAME)# define gl_rwlock_unlock(NAME) \    glthread_rwlock_unlock (&NAME)# define gl_rwlock_destroy(NAME) \    glthread_rwlock_destroy (&NAME)extern void glthread_rwlock_init (gl_rwlock_t *lock);extern void glthread_rwlock_rdlock (gl_rwlock_t *lock);extern void glthread_rwlock_wrlock (gl_rwlock_t *lock);extern void glthread_rwlock_unlock (gl_rwlock_t *lock);extern void glthread_rwlock_destroy (gl_rwlock_t *lock);/* --------------------- gl_recursive_lock_t datatype --------------------- *//* The Win32 documentation says that CRITICAL_SECTION already implements a   recursive lock.  But we need not rely on it: It's easy to implement a   recursive lock without this assumption.  */typedef struct        {          gl_spinlock_t guard; /* protects the initialization */          DWORD owner;          unsigned long depth;          CRITICAL_SECTION lock;        }        gl_recursive_lock_t;# define gl_recursive_lock_define(STORAGECLASS, NAME) \    STORAGECLASS gl_recursive_lock_t NAME;# define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \    STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;# define gl_recursive_lock_initializer \    { { 0, -1 }, 0, 0 }# define gl_recursive_lock_init(NAME) \    glthread_recursive_lock_init (&NAME)# define gl_recursive_lock_lock(NAME) \    glthread_recursive_lock_lock (&NAME)# define gl_recursive_lock_unlock(NAME) \    glthread_recursive_lock_unlock (&NAME)# define gl_recursive_lock_destroy(NAME) \    glthread_recursive_lock_destroy (&NAME)extern void glthread_recursive_lock_init (gl_recursive_lock_t *lock);extern void glthread_recursive_lock_lock (gl_recursive_lock_t *lock);extern void glthread_recursive_lock_unlock (gl_recursive_lock_t *lock);extern void glthread_recursive_lock_destroy (gl_recursive_lock_t *lock);/* -------------------------- gl_once_t datatype -------------------------- */typedef struct        {          volatile int inited;          volatile long started;          CRITICAL_SECTION lock;        }        gl_once_t;# define gl_once_define(STORAGECLASS, NAME) \    STORAGECLASS gl_once_t NAME = { -1, -1 };# define gl_once(NAME, INITFUNCTION) \    glthread_once (&NAME, INITFUNCTION)extern void glthread_once (gl_once_t *once_control, void (*initfunction) (void));# ifdef __cplusplus}# endif#endif/* ========================================================================= */#if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WIN32_THREADS)/* Provide dummy implementation if threads are not supported.  *//* -------------------------- gl_lock_t datatype -------------------------- */typedef int gl_lock_t;# define gl_lock_define(STORAGECLASS, NAME)# define gl_lock_define_initialized(STORAGECLASS, NAME)# define gl_lock_init(NAME)# define gl_lock_lock(NAME)# define gl_lock_unlock(NAME)/* ------------------------- gl_rwlock_t datatype ------------------------- */typedef int gl_rwlock_t;# define gl_rwlock_define(STORAGECLASS, NAME)# define gl_rwlock_define_initialized(STORAGECLASS, NAME)# define gl_rwlock_init(NAME)# define gl_rwlock_rdlock(NAME)# define gl_rwlock_wrlock(NAME)# define gl_rwlock_unlock(NAME)/* --------------------- gl_recursive_lock_t datatype --------------------- */typedef int gl_recursive_lock_t;# define gl_recursive_lock_define(STORAGECLASS, NAME)# define gl_recursive_lock_define_initialized(STORAGECLASS, NAME)# define gl_recursive_lock_init(NAME)# define gl_recursive_lock_lock(NAME)# define gl_recursive_lock_unlock(NAME)/* -------------------------- gl_once_t datatype -------------------------- */typedef int gl_once_t;# define gl_once_define(STORAGECLASS, NAME) \    STORAGECLASS gl_once_t NAME = 0;# define gl_once(NAME, INITFUNCTION) \    do                       \      {                      \        if (NAME == 0)       \          {                  \            NAME = ~ 0;      \            INITFUNCTION (); \          }                  \      }                      \    while (0)#endif/* ========================================================================= */#endif /* _LOCK_H */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -