"Fossies" - the Fresh Open Source Software Archive

Member "bonnie++-2.00a/semaphore.cpp" (23 Dec 2008, 2364 Bytes) of package /linux/privat/bonnie++-2.00a.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "semaphore.cpp" see the Fossies "Dox" file reference documentation.

    1 #include "port.h"
    2 #include <stdio.h>
    3 #include "semaphore.h"
    4 #include <unistd.h>
    5 #include <stdlib.h>
    6 
    7 Semaphore::Semaphore(int semKey, int numSems, int val)
    8  : m_semid(0)
    9  , m_semflg(IPC_CREAT | 0666)
   10  , m_semopen(false)
   11  , m_semKey(semKey)
   12  , m_numSems(numSems)
   13 {
   14   m_arg.val = (0);
   15   if(val)
   16   {
   17     if(create(val))
   18       exit(1);
   19   }
   20 }
   21 
   22 int Semaphore::clear_sem()
   23 {
   24   int semid;
   25   // have num-sems set to 1 so that we remove regardless of how many
   26   // semaphores were present.
   27   if((semid = semget(m_semKey, 1, 0666)) == -1)
   28   {
   29     perror("Can't get semaphore ID");
   30     return 1;
   31   }
   32   if(semctl(semid, 0, IPC_RMID, m_arg) == -1)
   33   {
   34     perror("Can't get remove semaphore");
   35     return 1;
   36   }
   37   printf("Semaphore removed.\n");
   38   return 0;
   39 }
   40 
   41 int Semaphore::create(int count)
   42 {
   43   if((m_semid = semget(m_semKey, m_numSems, m_semflg)) == -1)
   44   {
   45     perror("Can't get semaphore");
   46     return 1;
   47   }
   48   m_arg.val = count;
   49   int i;
   50   for(i = 0; i < m_numSems; ++i)
   51   {
   52     if(semctl(m_semid, i, SETVAL, m_arg) == -1)
   53     {
   54       perror("Can't set semaphore value");
   55       return 1;
   56     }
   57   }
   58   m_semopen = true;
   59   printf("Creating semaphore for %d procs.\n", count);
   60   return 0;
   61 }
   62 
   63 int Semaphore::get_semid()
   64 {
   65   int semflg = 0666;
   66   if ((m_semid = semget(m_semKey, m_numSems, semflg)) == -1)
   67   {
   68     perror("Can't get semaphore ID");
   69     return 1;
   70   }
   71   m_semopen = true;
   72   return 0;
   73 }
   74 
   75 int Semaphore::decrement_and_wait(int nr_sem)
   76 {
   77   if(!m_semopen)
   78     return 0;
   79   struct sembuf sops;
   80   sops.sem_num = nr_sem;
   81   sops.sem_op = -1;
   82   sops.sem_flg = IPC_NOWAIT;
   83   if(semop(m_semid, &sops, 1) == -1)
   84   {
   85     perror("semop: semop failed.\n");
   86     return 1;
   87   }
   88   sops.sem_num = nr_sem;
   89   sops.sem_op = 0;
   90   sops.sem_flg = SEM_UNDO;
   91   if(semop(m_semid, &sops, 1) == -1)
   92   {
   93     perror("semop: semop failed.\n");
   94     return 1;
   95   }
   96   return 0;
   97 }
   98 
   99 int Semaphore::get_mutex()
  100 {
  101   if(!m_semopen)
  102     return 0;
  103   struct sembuf sops;
  104   sops.sem_num = 0;
  105   sops.sem_op = -1;
  106   sops.sem_flg = SEM_UNDO;
  107   if(semop(m_semid, &sops, 1) == -1)
  108   {
  109     perror("semop: semop failed.\n");
  110     return 1;
  111   }
  112   return 0;
  113 }
  114 
  115 int Semaphore::put_mutex()
  116 {
  117   if(!m_semopen)
  118     return 0;
  119   struct sembuf sops;
  120   sops.sem_num = 0;
  121   sops.sem_op = 1;
  122   sops.sem_flg = IPC_NOWAIT;
  123   if(semop(m_semid, &sops, 1) == -1)
  124   {
  125     perror("semop: semop failed.\n");
  126     return 1;
  127   }
  128   return 0;
  129 }