1 /* thr_pthread.h
2  *  Copyright (C) 2001-2003, Parrot Foundation.
3  *  Overview:
4  *     POSIX pthread interface
5  *  Data Structure and Algorithms:
6  *  History:
7  *     2003.12.14  Initial rev by leo
8  *                 Moved common code from generic, darwin, openbsd
9  *                 to this file.
10  *  Notes:
11  *  References:
12  */
13 
14 #ifndef PARROT_THR_PTHREAD_H_GUARD
15 #define PARROT_THR_PTHREAD_H_GUARD
16 
17 #  include <pthread.h>
18 
19 #  define LOCK(m) pthread_mutex_lock(&(m))
20 #  define UNLOCK(m) pthread_mutex_unlock(&(m))
21 #  define COND_WAIT(c, m) pthread_cond_wait(&(c), &(m))
22 #  define COND_TIMED_WAIT(c, m, t, rc) \
23     do { (rc) = pthread_cond_timedwait(&(c), &(m), (t)); } while (0)
24 #  define COND_SIGNAL(c) pthread_cond_signal(&(c))
25 #  define COND_BROADCAST(c) pthread_cond_broadcast(&(c))
26 
27 /*
28  * for now use a fast mutex w/o error checking and non recursive
29  */
30 #  define MUTEX_INIT(m) pthread_mutex_init(&(m), NULL)
31 #  define MUTEX_DESTROY(m) pthread_mutex_destroy(&(m))
32 
33 #  define COND_INIT(c)    pthread_cond_init(&(c), NULL);
34 #  define COND_DESTROY(c) pthread_cond_destroy(&(c))
35 
36 #  define THREAD_CREATE_DETACHED(t, func, arg) \
37     do { \
38         pthread_attr_t      attr;   \
39         int rc = pthread_attr_init(&attr);      \
40         PARROT_ASSERT(rc == 0);    \
41         rc = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);   \
42         PARROT_ASSERT(rc == 0);    \
43         rc = pthread_create(&(t), &attr, (func), (arg)); \
44         PARROT_ASSERT(rc == 0);    \
45         pthread_attr_destroy(&attr);        \
46    } while (0)
47 
48 #  define THREAD_CREATE_JOINABLE(t, func, arg) \
49         pthread_create(&(t), NULL, (func), (arg))
50 
51 #  define JOIN(t, ret) pthread_join((t), &(ret))
52 #  define DETACH(t)    pthread_detach(t)
53 
54 #  define CLEANUP_PUSH(f, a) pthread_cleanup_push((f), (a))
55 #  define CLEANUP_POP(a)     pthread_cleanup_pop(a)
56 
57 #ifdef PARROT_HAS_HEADER_UNISTD
58 #  include <unistd.h>
59 #  ifdef _POSIX_PRIORITY_SCHEDULING
60 #    define YIELD sched_yield()
61 #  endif
62 #endif /* PARROT_HAS_HEADER_UNISTD */
63 
64 typedef pthread_mutex_t Parrot_mutex;
65 typedef pthread_cond_t Parrot_cond;
66 typedef pthread_t Parrot_thread;
67 
68 typedef void (*Cleanup_Handler)(void *);
69 
70 #endif /* PARROT_THR_PTHREAD_H_GUARD */
71 
72 /*
73  * Local variables:
74  *   c-file-style: "parrot"
75  * End:
76  * vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
77  */
78