You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.8 KiB

  1. #include <stdlib.h>
  2. #include <lwp/lwp.h>
  3. #include <lwp/stackdep.h>
  4. #define STACKSIZE 1000 /* stacksize for a thread */
  5. #define NSTACKS 2 /* # stacks to be put in cache initially */
  6. struct lock {
  7. int lock_locked;
  8. cv_t lock_condvar;
  9. mon_t lock_monitor;
  10. };
  11. /*
  12. * Initialization.
  13. */
  14. static void PyThread__init_thread(void)
  15. {
  16. lwp_setstkcache(STACKSIZE, NSTACKS);
  17. }
  18. /*
  19. * Thread support.
  20. */
  21. long PyThread_start_new_thread(void (*func)(void *), void *arg)
  22. {
  23. thread_t tid;
  24. int success;
  25. dprintf(("PyThread_start_new_thread called\n"));
  26. if (!initialized)
  27. PyThread_init_thread();
  28. success = lwp_create(&tid, func, MINPRIO, 0, lwp_newstk(), 1, arg);
  29. return success < 0 ? -1 : 0;
  30. }
  31. long PyThread_get_thread_ident(void)
  32. {
  33. thread_t tid;
  34. if (!initialized)
  35. PyThread_init_thread();
  36. if (lwp_self(&tid) < 0)
  37. return -1;
  38. return tid.thread_id;
  39. }
  40. void PyThread_exit_thread(void)
  41. {
  42. dprintf(("PyThread_exit_thread called\n"));
  43. if (!initialized)
  44. exit(0);
  45. lwp_destroy(SELF);
  46. }
  47. /*
  48. * Lock support.
  49. */
  50. PyThread_type_lock PyThread_allocate_lock(void)
  51. {
  52. struct lock *lock;
  53. extern char *malloc(size_t);
  54. dprintf(("PyThread_allocate_lock called\n"));
  55. if (!initialized)
  56. PyThread_init_thread();
  57. lock = (struct lock *) malloc(sizeof(struct lock));
  58. lock->lock_locked = 0;
  59. (void) mon_create(&lock->lock_monitor);
  60. (void) cv_create(&lock->lock_condvar, lock->lock_monitor);
  61. dprintf(("PyThread_allocate_lock() -> %p\n", lock));
  62. return (PyThread_type_lock) lock;
  63. }
  64. void PyThread_free_lock(PyThread_type_lock lock)
  65. {
  66. dprintf(("PyThread_free_lock(%p) called\n", lock));
  67. mon_destroy(((struct lock *) lock)->lock_monitor);
  68. free((char *) lock);
  69. }
  70. int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
  71. {
  72. int success;
  73. dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
  74. success = 0;
  75. (void) mon_enter(((struct lock *) lock)->lock_monitor);
  76. if (waitflag)
  77. while (((struct lock *) lock)->lock_locked)
  78. cv_wait(((struct lock *) lock)->lock_condvar);
  79. if (!((struct lock *) lock)->lock_locked) {
  80. success = 1;
  81. ((struct lock *) lock)->lock_locked = 1;
  82. }
  83. cv_broadcast(((struct lock *) lock)->lock_condvar);
  84. mon_exit(((struct lock *) lock)->lock_monitor);
  85. dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
  86. return success;
  87. }
  88. void PyThread_release_lock(PyThread_type_lock lock)
  89. {
  90. dprintf(("PyThread_release_lock(%p) called\n", lock));
  91. (void) mon_enter(((struct lock *) lock)->lock_monitor);
  92. ((struct lock *) lock)->lock_locked = 0;
  93. cv_broadcast(((struct lock *) lock)->lock_condvar);
  94. mon_exit(((struct lock *) lock)->lock_monitor);
  95. }