aboutsummaryrefslogtreecommitdiff
path: root/src/benchmarks/falsesharing/fred.h
blob: b0198a79b4390e257bd15e39d0dbb43552ce3dfa (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// -*- C++ -*-

#ifndef HL_FRED_H
#define HL_FRED_H

/// A thread-wrapper of childlike simplicity :).

#if defined(_WIN32)

  #include <windows.h>
  #include <process.h>

#elif defined(__SVR4)

  #include <thread.h>
  #include <pthread.h>
  #include <unistd.h>

#else

  #include <pthread.h>
  #include <unistd.h>

#endif

typedef void * (*ThreadFunctionType) (void *);

namespace HL {

class Fred {
public:

  Fred() {
#if !defined(_WIN32)
    pthread_attr_init (&attr);
    pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
#endif
  }

  ~Fred() {
#if !defined(_WIN32)
    pthread_attr_destroy (&attr);
#endif
  }

  void create (ThreadFunctionType function, void * arg) {
#if defined(_WIN32)
    t = CreateThread (0, 0, (LPTHREAD_START_ROUTINE) *function, (LPVOID) arg, 0, 0);
#else
    pthread_create (&t, &attr, function, arg);
#endif
  }

  void join (void) {
#if defined(_WIN32)
    WaitForSingleObject (t, INFINITE);
#else
    pthread_join (t, NULL);
#endif
  }

  static void yield (void) {
#if defined(_WIN32)
    Sleep (0);
#elif defined(__SVR4)
    thr_yield();
#else
    sched_yield();
#endif
  }


  static void setConcurrency (int n) {
#if defined(_WIN32)
#elif defined(__SVR4)
    thr_setconcurrency (n);
#else
    pthread_setconcurrency (n);
#endif
  }


private:
#if defined(_WIN32)
  typedef HANDLE FredType;
#else
  typedef pthread_t FredType;
  pthread_attr_t attr;
#endif

  FredType t;
};

}


#endif