File: test_pthread_clock_drift.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 121,860 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,366; asm: 2,415; lisp: 1,869
file content (62 lines) | stat: -rw-r--r-- 1,738 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2018 The Emscripten Authors.  All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License.  Both these licenses can be
// found in the LICENSE file.

#include <assert.h>
#include <pthread.h>
#include <emscripten.h>
#include <emscripten/threading.h>
#include <math.h>
#include <stdio.h>

volatile int threadStarted = 0;
volatile int timeReceived = 0;
volatile double mainThreadTime;

void wait(volatile int *address) {
  int state = emscripten_atomic_load_u32((void*)address);
  while (state == 0) {
    state = emscripten_atomic_load_u32((void*)address);
  }
}

void wake(volatile int *address) {
  emscripten_atomic_store_u32((void*)address, 1);
}

void *thread_main(void *arg) {
  wake(&threadStarted);
  wait(&timeReceived);
  double pthreadTime = emscripten_get_now();
  double timeDifference = pthreadTime - mainThreadTime;
  printf("Time difference between pthread and main thread is %f msecs.\n", timeDifference);

  // The time difference here should be well less than 1 msec, but test against
  // 200msecs to be super-sure.
  assert(fabs(timeDifference) < 200);
  emscripten_force_exit(0);
  return 0;
}

void busy_sleep(double msecs) {
  double end = emscripten_get_now() + msecs;
  while (emscripten_get_now() < end) {
    // busy loop
  }
}

int main()
{
  // Cause a one second delay between main() and pthread start that might have a
  // chance to drift the wallclocks on emscripten_get_now().
  busy_sleep(1000);

  pthread_t thread;
  pthread_create(&thread, NULL, thread_main, NULL);
  wait(&threadStarted);
  mainThreadTime = emscripten_get_now();
  wake(&timeReceived);

  emscripten_exit_with_live_runtime();
}