Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef __MYTHREADS_h__
  2. #define __MYTHREADS_h__
  3. #include <pthread.h>
  4. #include <assert.h>
  5. #include <sys/time.h>
  6. #include <stdlib.h>
  7. void *Malloc(size_t size) {
  8. void *p = malloc(size);
  9. assert(p != NULL);
  10. return p;
  11. }
  12. double Time_GetSeconds() {
  13. struct timeval t;
  14. int rc = gettimeofday(&t, NULL);
  15. assert(rc == 0);
  16. return (double) ((double)t.tv_sec + (double)t.tv_usec / 1e6);
  17. }
  18. void work(int seconds) {
  19. double t0 = Time_GetSeconds();
  20. while ((Time_GetSeconds() - t0) < (double)seconds)
  21. ;
  22. }
  23. void Mutex_init(pthread_mutex_t *m) {
  24. assert(pthread_mutex_init(m, NULL) == 0);
  25. }
  26. void Mutex_lock(pthread_mutex_t *m) {
  27. int rc = pthread_mutex_lock(m);
  28. assert(rc == 0);
  29. }
  30. void Mutex_unlock(pthread_mutex_t *m) {
  31. int rc = pthread_mutex_unlock(m);
  32. assert(rc == 0);
  33. }
  34. void Cond_init(pthread_cond_t *c) {
  35. assert(pthread_cond_init(c, NULL) == 0);
  36. }
  37. void Cond_wait(pthread_cond_t *c, pthread_mutex_t *m) {
  38. int rc = pthread_cond_wait(c, m);
  39. assert(rc == 0);
  40. }
  41. void Cond_signal(pthread_cond_t *c) {
  42. int rc = pthread_cond_signal(c);
  43. assert(rc == 0);
  44. }
  45. void Pthread_create(pthread_t *thread, const pthread_attr_t *attr,
  46. void *(*start_routine)(void*), void *arg) {
  47. int rc = pthread_create(thread, attr, start_routine, arg);
  48. assert(rc == 0);
  49. }
  50. void Pthread_join(pthread_t thread, void **value_ptr) {
  51. int rc = pthread_join(thread, value_ptr);
  52. assert(rc == 0);
  53. }
  54. #endif // __MYTHREADS_h__