home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / DEV.Z / cthreads.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-08  |  3.4 KB  |  128 lines

  1. /*
  2.  * Copyright (c) 1995, NeXT Computer, Inc.
  3.  * All Rights Reserved.
  4.  */
  5.  
  6. #ifndef __MACHEMUL_CTHREADS_H
  7. #define __MACHEMUL_CTHREADS_H
  8.  
  9. #include <mach/mach.h>
  10. #include <mach/ARCH_INCLUDE.h>
  11. #include OS_INCLUDE(mach/, cthreads.h)
  12.  
  13.  
  14.  
  15. typedef any_t (*cthread_fn_t) (any_t arg);
  16. typedef struct cthread *cthread_t;
  17.  
  18.  
  19. #ifdef CTHREADS_AVAILABLE
  20.  
  21. /*
  22.  * Mutex variables
  23.  */
  24.  
  25. MACHEXPORT mutex_t mutex_alloc (void);
  26. MACHEXPORT void mutex_free (mutex_t m);
  27. MACHEXPORT void mutex_init (mutex_t m);
  28. MACHEXPORT void mutex_clear (mutex_t m);
  29. MACHEXPORT void mutex_lock (mutex_t m);
  30. MACHEXPORT void mutex_unlock (mutex_t m);
  31.  
  32.  
  33.  
  34.  
  35. /*
  36.  * Condition variables
  37.  */
  38.  
  39. MACHEXPORT condition_t condition_alloc (void);
  40. MACHEXPORT void condition_free (condition_t c);
  41. MACHEXPORT void condition_init (condition_t c);
  42. MACHEXPORT void condition_clear (condition_t c);
  43. MACHEXPORT void condition_wait (condition_t c, mutex_t m);
  44. MACHEXPORT int condition_wait_timeout (condition_t c, mutex_t m, double timeout);
  45. MACHEXPORT void condition_broadcast (condition_t c);
  46. MACHEXPORT void condition_signal (condition_t c);
  47.  
  48.  
  49.  
  50.  
  51. /*
  52.  * Threads
  53.  */
  54.  
  55. typedef struct cthread_callback *cthread_callback_t;
  56.  
  57. struct cthread {
  58.     native_thread_t thread;        // The native thread.
  59.     native_key_t key;            // Native key (for thread death)
  60.     cthread_fn_t function;        // User function.
  61.     any_t arg;                // Arg to the user function.
  62.     any_t status;            // Exit status.
  63.     any_t data;                // Thread data (cthread_set_data).
  64.     unsigned char main:1;        // Main thread?
  65.     unsigned char terminated:1;        // Thread terminated?
  66.     unsigned char detached:1;        // Thread detached?
  67.     unsigned char joining:1;        // Join waiting for termination?
  68.     cthread_callback_t callback;    // Thread death callback
  69.     any_t callback_arg;            // Callback function argument
  70.     struct mutex lock;            // Internal lock.
  71.     struct condition condition;        // Internal condition.
  72.     struct cthread *next;        // Next thread in list.
  73.     port_t mig_reply_port;        // MiG reply port
  74. };
  75.  
  76. MACHEXPORT cthread_t cthread_fork (cthread_fn_t function, any_t arg);
  77. MACHEXPORT void cthread_detach (cthread_t t);
  78. MACHEXPORT any_t cthread_join (cthread_t t);
  79. MACHEXPORT void cthread_yield (void);
  80. MACHEXPORT void cthread_exit (any_t result);
  81. MACHEXPORT kern_return_t cthread_abort (cthread_t t);
  82. MACHEXPORT cthread_t cthread_self (void);
  83. MACHEXPORT native_thread_t *cthread_thread (cthread_t t);
  84. MACHEXPORT void cthread_set_data (cthread_t t, any_t data);
  85. MACHEXPORT any_t cthread_data (cthread_t t);
  86. MACHEXPORT void cthread_set_name (cthread_t t, const char *name);
  87.  
  88.  
  89. #else CTHREADS_AVAILABLE
  90.  
  91.  
  92. /*
  93.  * cthreads disabled for the single-threaded version of the library.
  94.  */
  95.  
  96. #define mutex_alloc() (mutex_t)0x00000004
  97. #define mutex_free(m)
  98. #define mutex_init(m)
  99. #define mutex_clear(m)
  100. #define mutex_lock(m)
  101. #define mutex_unlock(m)
  102.  
  103. #define condition_alloc() (condition_t)0x00000004
  104. #define condition_free(c)
  105. #define condition_init(c)
  106. #define condition_clear(c)
  107. #define condition_wait(c, m)
  108. #define condition_wait_timeout(c, m, t)    0
  109. #define condition_signal(c)
  110. #define condition_broadcast(c)
  111.  
  112. #define cthread_fork(f, a) (cthread_t)0
  113. #define cthread_detach(t)
  114. #define cthread_join(t) (cthread_t)0
  115. #define cthread_yield()
  116. #define cthread_exit(r)
  117. #define cthread_abort(t) KERN_FAILURE
  118. #define cthread_self() (cthread_t)0x00000004
  119. #define cthread_thread (void *)0
  120.  
  121. extern any_t __cthread_data;
  122. #define cthread_set_data(t, d) (__cthread_data = (d))
  123. #define cthread_data(t) (__cthread_data)
  124.  
  125. #endif CTHREADS_AVAILABLE
  126.  
  127. #endif __MACHEMUL_CTHREADS_H
  128.