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.

130 lines
2.6 KiB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include </usr/include/thread.h>
  5. #undef _POSIX_THREADS
  6. /*
  7. * Initialization.
  8. */
  9. static void PyThread__init_thread(void)
  10. {
  11. }
  12. /*
  13. * Thread support.
  14. */
  15. struct func_arg {
  16. void (*func)(void *);
  17. void *arg;
  18. };
  19. static void *
  20. new_func(void *funcarg)
  21. {
  22. void (*func)(void *);
  23. void *arg;
  24. func = ((struct func_arg *) funcarg)->func;
  25. arg = ((struct func_arg *) funcarg)->arg;
  26. free(funcarg);
  27. (*func)(arg);
  28. return 0;
  29. }
  30. long
  31. PyThread_start_new_thread(void (*func)(void *), void *arg)
  32. {
  33. thread_t tid;
  34. struct func_arg *funcarg;
  35. dprintf(("PyThread_start_new_thread called\n"));
  36. if (!initialized)
  37. PyThread_init_thread();
  38. funcarg = (struct func_arg *) malloc(sizeof(struct func_arg));
  39. funcarg->func = func;
  40. funcarg->arg = arg;
  41. if (thr_create(0, 0, new_func, funcarg,
  42. THR_DETACHED | THR_NEW_LWP, &tid)) {
  43. perror("thr_create");
  44. free((void *) funcarg);
  45. return -1;
  46. }
  47. return tid;
  48. }
  49. long
  50. PyThread_get_thread_ident(void)
  51. {
  52. if (!initialized)
  53. PyThread_init_thread();
  54. return thr_self();
  55. }
  56. void
  57. PyThread_exit_thread(void)
  58. {
  59. dprintf(("PyThread_exit_thread called\n"));
  60. if (!initialized)
  61. exit(0);
  62. thr_exit(0);
  63. }
  64. /*
  65. * Lock support.
  66. */
  67. PyThread_type_lock
  68. PyThread_allocate_lock(void)
  69. {
  70. mutex_t *lock;
  71. dprintf(("PyThread_allocate_lock called\n"));
  72. if (!initialized)
  73. PyThread_init_thread();
  74. lock = (mutex_t *) malloc(sizeof(mutex_t));
  75. if (mutex_init(lock, USYNC_THREAD, 0)) {
  76. perror("mutex_init");
  77. free((void *) lock);
  78. lock = 0;
  79. }
  80. dprintf(("PyThread_allocate_lock() -> %p\n", lock));
  81. return (PyThread_type_lock) lock;
  82. }
  83. void
  84. PyThread_free_lock(PyThread_type_lock lock)
  85. {
  86. dprintf(("PyThread_free_lock(%p) called\n", lock));
  87. mutex_destroy((mutex_t *) lock);
  88. free((void *) lock);
  89. }
  90. int
  91. PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
  92. {
  93. int success;
  94. dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
  95. if (waitflag)
  96. success = mutex_lock((mutex_t *) lock);
  97. else
  98. success = mutex_trylock((mutex_t *) lock);
  99. if (success < 0)
  100. perror(waitflag ? "mutex_lock" : "mutex_trylock");
  101. else
  102. success = !success; /* solaris does it the other way round */
  103. dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
  104. return success;
  105. }
  106. void
  107. PyThread_release_lock(PyThread_type_lock lock)
  108. {
  109. dprintf(("PyThread_release_lock(%p) called\n", lock));
  110. if (mutex_unlock((mutex_t *) lock))
  111. perror("mutex_unlock");
  112. }