1 : //=============================================================================
2 : // File <$/src/cpp/prod/sync/Spin.cpp>
3 : // This file is part of YaOrb : Yet Another Object Request Broker,
4 : // Copyright (c) 2000-2003, Marc Alff.
5 : //
6 : // This program is free software; you can redistribute it and/or
7 : // modify it under the terms of the GNU General Public License
8 : // as published by the Free Software Foundation; either version 2
9 : // of the License, or (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program; if not, write to the Free Software
18 : // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 : //
20 : //=============================================================================
21 :
22 : // Portability
23 : #include "yaorb/config.h"
24 : #include "src/cpp/prod/port/port_unistd.h"
25 :
26 : #include <asm/bitops.h>
27 : #include <iostream>
28 :
29 : #include "src/cpp/prod/sync/Spin.h"
30 : #include "src/cpp/prod/tool/Assert.h"
31 :
32 : // A SPIN is supposed to be locked for a SHORT time.
33 : // Complains will be emited every 0.1 second
34 :
35 : // FIXME : move to dimensioning.h ? or leave here ...
36 : const int UPSET_LIMIT = 100 ;
37 : const int USECOND_RETRY_DELAY = 1000 ;
38 :
39 12 : Spin::Spin()
40 : : _real_spin(0),
41 : _file(),
42 12 : _line(0)
43 : {
44 12 : clear_bit(0, &_real_spin) ;
45 : }
46 :
47 4 : Spin::~Spin()
48 : {
49 8 : if (test_bit(0, &_real_spin) != 0)
50 : {
51 : std::cerr << "Destroying spin locked by " << _file
52 0 : << ", at " << _line << " !" << std::endl ;
53 : }
54 4 : }
55 :
56 : Locker::Locker(
57 : const char* file,
58 : int line,
59 10 : Spin& spin)
60 10 : : _file(file), _line(line), _spin(spin)
61 : {
62 : int lock ;
63 : int count = 0 ;
64 :
65 : do
66 : {
67 10 : lock = test_and_set_bit(0, & (_spin._real_spin)) ;
68 :
69 10 : if (lock != 0)
70 : {
71 0 : count ++ ;
72 :
73 0 : if ((count % UPSET_LIMIT) == 0)
74 : {
75 : std::cerr << "Can't get a lock after "
76 0 : << count << " attempts !" << std::endl ;
77 : std::cerr << "Locking from " << _file
78 0 : << ", at " << _line << "." << std::endl ;
79 : std::cerr << "Spin locked by " << _spin._file
80 0 : << ", at " << _spin._line << "." << std::endl ;
81 : }
82 0 : usleep(USECOND_RETRY_DELAY) ;
83 : }
84 :
85 : }
86 : while (lock != 0) ;
87 :
88 10 : spin._file = _file ;
89 10 : spin._line = _line ;
90 : }
91 :
92 10 : Locker::~Locker()
93 : {
94 : ASSERT(_spin._file == _file) ;
95 : ASSERT(_spin._line == _line) ;
96 :
97 : ASSERT(test_bit(0, &(_spin._real_spin)) != 0) ;
98 :
99 10 : _spin._file = "" ;
100 10 : _spin._line = 0 ;
101 :
102 10 : clear_bit(0, & (_spin._real_spin)) ;
103 49 : }
104 49 :
|