1
0

test-barrier.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <string.h>
  24. #include <errno.h>
  25. typedef struct {
  26. uv_barrier_t barrier;
  27. int delay;
  28. volatile int posted;
  29. } worker_config;
  30. static void worker(void* arg) {
  31. worker_config* c = arg;
  32. if (c->delay)
  33. uv_sleep(c->delay);
  34. uv_barrier_wait(&c->barrier);
  35. }
  36. TEST_IMPL(barrier_1) {
  37. uv_thread_t thread;
  38. worker_config wc;
  39. memset(&wc, 0, sizeof(wc));
  40. ASSERT(0 == uv_barrier_init(&wc.barrier, 2));
  41. ASSERT(0 == uv_thread_create(&thread, worker, &wc));
  42. uv_sleep(100);
  43. uv_barrier_wait(&wc.barrier);
  44. ASSERT(0 == uv_thread_join(&thread));
  45. uv_barrier_destroy(&wc.barrier);
  46. return 0;
  47. }
  48. TEST_IMPL(barrier_2) {
  49. uv_thread_t thread;
  50. worker_config wc;
  51. memset(&wc, 0, sizeof(wc));
  52. wc.delay = 100;
  53. ASSERT(0 == uv_barrier_init(&wc.barrier, 2));
  54. ASSERT(0 == uv_thread_create(&thread, worker, &wc));
  55. uv_barrier_wait(&wc.barrier);
  56. ASSERT(0 == uv_thread_join(&thread));
  57. uv_barrier_destroy(&wc.barrier);
  58. return 0;
  59. }
  60. TEST_IMPL(barrier_3) {
  61. uv_thread_t thread;
  62. worker_config wc;
  63. memset(&wc, 0, sizeof(wc));
  64. ASSERT(0 == uv_barrier_init(&wc.barrier, 2));
  65. ASSERT(0 == uv_thread_create(&thread, worker, &wc));
  66. uv_barrier_wait(&wc.barrier);
  67. ASSERT(0 == uv_thread_join(&thread));
  68. uv_barrier_destroy(&wc.barrier);
  69. return 0;
  70. }