forked from ghaerr/microwindows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock.h
More file actions
83 lines (78 loc) · 2.45 KB
/
Copy pathlock.h
File metadata and controls
83 lines (78 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Critical section locking definitions for Microwindows
* Copyright (c) 2002, 2003 by Greg Haerr <greg@censoft.com>
*
* The current implementation uses pthreads included in libc
*
* It's currently required that any locking mechanism
* allow multiple locks on the same thread (ie. recursive calls)
* This is necessary since routines nest calls on
* LOCK(&nxGlobalLock). (nanox/client.c and nanox/nxproto.c)
*/
#if THREADSAFE
#define THREADSAFE_LINUX 1 /* use linux threadsafe routines*/
#endif
/*
* Linux critical section locking definitions
*/
#if THREADSAFE_LINUX
#define __USE_GNU /* define _NP routines*/
#include <pthread.h>
typedef pthread_mutex_t MWMUTEX;
#if ! (defined(__CYGWIN__) | RTEMS | MACOSX)
/*
* This definition doesn't require explicit initialization and -lpthread
*
* It uses a common (but non-standard) pthreads extension.
*/
#define LOCK_DECLARE(name) MWMUTEX name = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
#define LOCK_INIT(m)
#else
/*
* This definition requires adding -lpthreads to link all Nano-X applications
* which isn't required if LOCK_DECLARE is used as above: The pthread entry
* points pthread_mutex_lock/unlock are included in the standard C library, but
* pthread_mutex_init is not. If this is not the case with your library,
* include these routines, and add -lpthreads to your applications link line.
*/
#define LOCK_DECLARE(name) MWMUTEX name
#if 1
/*
* Use portable version.
*
* Note: Older libraries may not have these UNIX98 functions. You may need
* to use the old non-portable function name (see below).
*/
#define LOCK_INIT(m) \
{ \
pthread_mutexattr_t attr; \
pthread_mutexattr_init(&attr); \
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
pthread_mutex_init((m), &attr); \
}
#else
/* Use old non-portable function name */
#define LOCK_INIT(m) \
{ \
pthread_mutexattr_t attr; \
pthread_mutexattr_init(&attr); \
pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP); \
pthread_mutex_init((m), &attr); \
}
#endif
#endif
#define LOCK_EXTERN(name) extern MWMUTEX name
#define LOCK_FREE(m) pthread_mutex_destroy(m)
#define LOCK(m) pthread_mutex_lock(m)
#define UNLOCK(m) pthread_mutex_unlock(m)
#endif /* THREADSAFE_LINUX*/
/* no locking support - dummy macros*/
#if !THREADSAFE
typedef int MWMUTEX;
#define LOCK_DECLARE(name) MWMUTEX name
#define LOCK_EXTERN(name) extern MWMUTEX name
#define LOCK_INIT(m)
#define LOCK_FREE(m)
#define LOCK(m)
#define UNLOCK(m)
#endif /* !THREADSAFE*/