source: ThirdParty/mpqc_open/src/lib/util/group/globcnt.cc@ 7516f6

Action_Thermostats Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.1 ChemicalSpaceEvaluator Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Exclude_Hydrogens_annealWithBondGraph Fix_Verbose_Codepatterns ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion Gui_displays_atomic_force_velocity JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool PythonUI_with_named_parameters Recreated_GuiChecks StoppableMakroAction TremoloParser_IncreasedPrecision
Last change on this file since 7516f6 was 860145, checked in by Frederik Heber <heber@…>, 8 years ago

Merge commit '0b990dfaa8c6007a996d030163a25f7f5fc8a7e7' as 'ThirdParty/mpqc_open'

  • Property mode set to 100644
File size: 3.2 KB
Line 
1//
2// globcnt.cc
3//
4// Copyright (C) 1996 Limit Point Systems, Inc.
5//
6// Author: Curtis Janssen <cljanss@limitpt.com>
7// Maintainer: LPS
8//
9// This file is part of the SC Toolkit.
10//
11// The SC Toolkit is free software; you can redistribute it and/or modify
12// it under the terms of the GNU Library General Public License as published by
13// the Free Software Foundation; either version 2, or (at your option)
14// any later version.
15//
16// The SC Toolkit is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Library General Public License for more details.
20//
21// You should have received a copy of the GNU Library General Public License
22// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24//
25// The U.S. Government is granted a limited license as per AL 91-7.
26//
27
28#ifdef __GNUG__
29#pragma implementation
30#endif
31
32#ifdef HAVE_CONFIG_H
33#include <scconfig.h>
34#endif
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/types.h>
40#include <sys/ipc.h>
41#include <sys/sem.h>
42#include <util/group/globcnt.h>
43
44using namespace sc;
45
46#ifndef SEM_A
47# define SEM_A 0200
48#endif
49
50#ifndef SEM_R
51# define SEM_R 0400
52#endif
53
54GlobalCounter::GlobalCounter()
55{
56 semid_ = -1;
57 controls_release_ = 0;
58}
59
60void
61GlobalCounter::cleanup()
62{
63 if (semid_ != -1 && controls_release_) {
64 int ret;
65#ifdef SEMCTL_REQUIRES_SEMUN
66 semun junk;
67 junk.val = 0;
68#else
69 int junk = 0;
70#endif
71 ret = semctl(semid_, 0, IPC_RMID, junk);
72 if (ret == -1) {
73 perror("semctl (IPC_RMID)");
74 abort();
75 }
76
77 semid_ = -1;
78 }
79}
80
81void
82GlobalCounter::initialize()
83{
84 cleanup();
85 semid_ = semget(IPC_PRIVATE, 1, IPC_CREAT | SEM_R | SEM_A );
86 if (semid_ == -1) {
87 perror("semget");
88 abort();
89 }
90 controls_release_ = 1;
91 operator = (0);
92}
93
94void
95GlobalCounter::initialize(const char *stringrep)
96{
97 semid_ = atoi(stringrep);
98 controls_release_ = 0;
99}
100
101GlobalCounter::~GlobalCounter()
102{
103 cleanup();
104}
105
106void
107GlobalCounter::operator = (int i)
108{
109#ifdef SEMCTL_REQUIRES_SEMUN
110 semun val;
111 val.val = i;
112#else
113 int val = i;
114#endif
115 if (semctl(semid_, 0, SETVAL, val) == -1) {
116 perror("semctl (SETVAL)");
117 abort();
118 }
119}
120
121int
122GlobalCounter::val()
123{
124#ifdef SEMCTL_REQUIRES_SEMUN
125 semun val;
126 val.val = 0;
127#else
128 int val = 0;
129#endif
130 int ret;
131 if ((ret = semctl(semid_, 0, GETVAL, val)) == -1) {
132 perror("semctl (GETVAL)");
133 abort();
134 }
135 return ret;
136}
137
138void
139GlobalCounter::wait_for_zero()
140{
141 operator += (0);
142}
143
144void
145GlobalCounter::operator+=(int i)
146{
147 struct sembuf s;
148 s.sem_num = 0;
149 s.sem_op = i;
150 s.sem_flg = 0;
151 if (semop(semid_, &s, 1) == -1) {
152 perror("semop");
153 abort();
154 }
155}
156
157void
158GlobalCounter::operator--()
159{
160 operator += (-1);
161}
162
163void
164GlobalCounter::operator++()
165{
166 operator += (1);
167}
168
169char *
170GlobalCounter::stringrep()
171{
172 char tmp[80];
173 sprintf(tmp, "%d", semid_);
174 return strcpy(new char[strlen(tmp)+1], tmp);
175}
176
177/////////////////////////////////////////////////////////////////////////////
178
179// Local Variables:
180// mode: c++
181// c-file-style: "CLJ"
182// End:
Note: See TracBrowser for help on using the repository browser.