1 /*****************************************************************************\
4 * Author : Chris Koeritz *
6 *******************************************************************************
7 * Copyright (c) 2001-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
15 //note: after repeated investigation, it seems that if we unlink the rendezvous
16 // file on destruction, then this hoses up any locks attempted ever after.
17 // instead of waiting for a lock, new attempts think they can go ahead,
18 // even though someone else might also have been given the lock. it seems
19 // we cannot remove the files without destroying the semantics.
21 #include "rendezvous.h"
23 #include <application/windoze_helper.h>
24 #include <basis/astring.h>
25 #include <basis/functions.h>
26 #include <basis/utf_conversion.h>
27 #include <filesystem/filename.h>
35 using namespace basis;
36 using namespace filesystem;
40 //#define DEBUG_RENDEZVOUS
41 // uncomment for a noisier file.
44 #define LOG(tp) printf("%s%s\n", time_stamp::notarize(true).s(), astring(tp).s())
45 // we can only use simple logging here since the rendezvous is relied on
46 // at very low levels and use of a log_base object would cause infinite
49 // used for the name of a mutex or part of the unix lock filename.
50 astring general_lock_name(const astring &root_part)
51 { return root_part + "_app_lock"; }
54 // the name of the locking file used in unix.
55 astring unix_rendez_file(const astring &lock_name)
57 astring clean_name = lock_name;
58 // remove troublesome characters from the name.
59 filename::detooth_filename(clean_name);
60 // make sure our target directory exists.
62 // use a system-wide location for rendezvous state files.
63 astring tmp_dir = "/tmp/rendezvous";
65 mkdir(tmp_dir.observe(), 0777);
66 return tmp_dir + "/ren_" + clean_name;
70 rendezvous::rendezvous(const astring &root_name)
71 : _handle(NULL_POINTER),
73 _root_name(new astring(root_name))
75 #ifdef DEBUG_RENDEZVOUS
76 FUNCDEF("constructor");
78 astring lock_name = general_lock_name(root_name);
80 astring real_name = unix_rendez_file(lock_name);
81 FILE *locking_file = fopen(real_name.s(), "wb");
83 #ifdef DEBUG_RENDEZVOUS
84 LOG(astring("failure to create locking file ") + real_name
85 + ": " + critical_events::system_error_text(critical_events::system_error()) );
90 _handle = locking_file;
93 _handle = CreateMutex(NULL_POINTER, false, to_unicode_temp(lock_name));
98 rendezvous::~rendezvous()
100 #ifdef DEBUG_RENDEZVOUS
101 FUNCDEF("destructor");
102 LOG("okay, into destructor for rendezvous.");
107 int ret = lockf(fileno((FILE *)_handle), F_ULOCK, sizeof(int));
109 #ifdef DEBUG_RENDEZVOUS
110 LOG("failure to get lock on file.");
113 _locked = false; // clear our locked status since we no longer have one.
115 //note: after repeated investigation, it seems that if we unlink the rendezvous
116 // file on destruction, then this hoses up any locks attempted ever after.
117 // instead of waiting for a lock, new attempts think they can go ahead,
118 // even though someone else might also have been given the lock. it seems
119 // we cannot remove the files without destroying the semantics.
122 fclose((FILE *)_handle);
123 _handle = NULL_POINTER;
127 if (_handle) CloseHandle((HANDLE)_handle);
132 void rendezvous::establish_lock() { lock(); }
134 void rendezvous::repeal_lock() { unlock(); }
136 bool rendezvous::healthy() const
141 bool rendezvous::lock(locking_methods how)
143 #ifdef DEBUG_RENDEZVOUS
146 if (how == NO_LOCKING) return false;
147 if (!healthy()) return false;
149 int command = F_TLOCK;
150 if (how == ENDLESS_WAIT) command = F_LOCK;
151 int ret = lockf(fileno((FILE *)_handle), command, sizeof(int));
153 #ifdef DEBUG_RENDEZVOUS
154 LOG("failure to get lock on file.");
158 #ifdef DEBUG_RENDEZVOUS
159 LOG("okay, got lock on shared mem.");
165 int timing = 0; // immediate return.
166 if (how == ENDLESS_WAIT) timing = INFINITE;
167 int ret = WaitForSingleObject((HANDLE)_handle, timing);
168 if ( (ret == WAIT_ABANDONED) || (ret == WAIT_TIMEOUT) ) return false;
169 else if (ret != WAIT_OBJECT_0) {
170 #ifdef DEBUG_RENDEZVOUS
171 LOG("got an unanticipated error from waiting for the mutex.");
181 void rendezvous::unlock()
183 #ifdef DEBUG_RENDEZVOUS
186 if (!healthy()) return;
189 int ret = lockf(fileno((FILE *)_handle), F_ULOCK, sizeof(int));
191 #ifdef DEBUG_RENDEZVOUS
192 LOG("failure to get lock on file.");
197 ReleaseMutex((HANDLE)_handle);
201 #ifdef DEBUG_RENDEZVOUS
202 LOG("okay, rendezvous wasn't locked.");