source: git/libthread/thread.cc @ 54b24c

spielwiese
Last change on this file since 54b24c was 54b24c, checked in by Reimer Behrends <behrends@…>, 5 years ago
Finalizing thread support.
  • Property mode set to 100644
File size: 993 bytes
Line 
1#include "threadconf.h"
2#ifdef ENABLE_THREADS
3#include <factory/prelude.h>
4#endif
5#include <list>
6#include <vector>
7
8#include <cstring>
9#include <stdio.h>
10#include <stdlib.h>
11#include <unistd.h>
12#include <sys/mman.h>
13
14#include "thread.h"
15#include "singthreads.h"
16
17using namespace std;
18
19pthread_t no_thread;
20
21void ThreadError(const char *message) {
22  fprintf(stderr, "FATAL ERROR: %s\n", message);
23  abort(); // should not happen
24}
25
26void Semaphore::wait() {
27  lock.lock();
28  waiting++;
29  while (count == 0)
30    cond.wait();
31  waiting--;
32  count--;
33  lock.unlock();
34}
35
36void Semaphore::post() {
37  lock.lock();
38  if (count++ == 0 && waiting)
39    cond.signal();
40  lock.unlock();
41}
42
43namespace LibThread {
44  template <typename T>
45  T *shared_alloc(size_t n) {
46    T *p = (T *) malloc(n * sizeof(T));
47    return p;
48  }
49  template <typename T>
50  T *shared_alloc0(size_t n) {
51    T *p = (T *) calloc(n, sizeof(T));
52    return p;
53  }
54  template <typename T>
55  void shared_free(T *p) {
56    free(p);
57  }
58}
Note: See TracBrowser for help on using the repository browser.