File: a_livepatch1.cpp

package info (click to toggle)
libpulp 0.3.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,976 kB
  • sloc: ansic: 11,792; python: 1,216; sh: 881; makefile: 871; cpp: 582; asm: 387
file content (96 lines) | stat: -rw-r--r-- 2,337 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
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
#include <semaphore.h>
#include <pthread.h>
#include <iostream>

#define QMAX 32

/** A producer-consumer queue.  This structure creates a channel in which
    two threads can communicate, one by enqueuing elements and another by
    dequeuing elements.  */
template<int MAX, typename T>
class Queue
{
  public:
  Queue(void);
  ~Queue(void);

  void Push_LP(T x);
  T Pop(void);

  protected:
  /** Position of the last inserted element.*/
  int head;

  /** Position of the oldest element in the queue.  */
  int tail;

  /** Semaphore that will block any attempt of dequeuing an element if the
      queue is empty.  */
  sem_t empty;

  /** Semaphore that will block any attempt of enqueuing an element if the
      queue is full.  */
  sem_t full;

  /** Lock for head & tail.  This is unnecessary if there is only one producer
      and one consumer.  If you wish to support many producers and many
      consumers, define MORE_THAN_ONE_ONE.  */
  pthread_mutex_t lock;

  /** The queue buffer.  */
  T elem[MAX];
};

static bool already_print_l = false;
static bool already_print_d = false;

template <int MAX, typename T>
void Queue<MAX, T>::Push_LP(T x)
{
  int ret;
  /* Block if the queue is full.  */
  ret = sem_wait(&full);
  if (ret != 0) {
    throw "semaphore error";
  }

  /* Acquire lock of queue.  */
  ret = pthread_mutex_lock(&lock);
  if (ret != 0) {
    throw "mutex lock error";
  }
  /* ----------------------------- */

  if (typeid(T) == typeid(long) && already_print_l == false) {
    std::cout << "from critical section with type long\n";
    already_print_l = true;
  } else if (typeid(T) == typeid(double) && already_print_d == false) {
    std::cout << "from critical section with type double\n";
    already_print_d = true;
  }

  elem[head++] = x;

  /* Wraps around if end of buffer.  */
  if (head >= MAX) {
    head = 0;
  }

  /* ----------------------------- */

  /* Release lock of queue.  */
  ret = pthread_mutex_unlock(&lock);
  if (ret != 0) {
    throw "mutex release error";
  }

  /* Alert other threads that we inserted something.  */
  ret = sem_post(&empty);
  if (ret != 0) {
    throw "semaphore post error";
  }
}

/* Output the modified functions for all types generated.  */
template void Queue<QMAX, long>::Push_LP(long x);
template void Queue<QMAX, double>::Push_LP(double x);