File: lockrunfile.cc

package info (click to toggle)
stealth 3.00.00-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,380 kB
  • ctags: 371
  • sloc: cpp: 2,730; makefile: 161; sh: 98
file content (59 lines) | stat: -rw-r--r-- 1,663 bytes parent folder | download
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
#include "lock.ih"

// In time: make a CFIle object allowing us to open a file, determine its
// file descriptor, and have it closed by its destructor. 

bool Lock::lockRunFile(LockType type)
try
{
//    if (s_runFILE)
//        fmsg << "Internal error: runfile already locked" << endl;
//
//    if (s_runFilename.empty())          // no runfilename, no lock.
//    {
//        imsg << "No need to lock a run file" << endl;
//        return true;
//    }

    imsg << "opening `" << s_runFilename << "' for reading" << endl;

    s_runFILE = fopen(s_runFilename.c_str(), "r");

    if (s_runFILE == 0)
        fmsg << "Can't open run-file `" << s_runFilename.c_str() << '\'' <<
                                                                        endl;

    if (type == BLOCKING)
    {
//        imsg << "attempting blocking mode lock" << endl;
//        if (flock(fileno(s_runFILE), LOCK_EX) == 0)
            throw true;
//        imsg << "blocking mode lock FAILED" << endl;
    }
    else
    {
        imsg << "attempting non-blocking mode lock on FD " << 
                                                    fileno(s_runFILE) << endl;
//        for (size_t idx = 0; idx < s_maxBlockAttempts; ++idx)
//        {
//            if (flock(fileno(s_runFILE), LOCK_EX  | LOCK_NB) == 0)
                throw true;
//            imsg << '.';
//            ::sleep(1);
//            imsg << "\nNon-blocking mode lock FAILED" << endl;
//        }
    }
    throw false;
}
catch (bool ret)
{
    imsg << "locked (and return): " << ret << endl;

//    if (!ret)
//        fmsg << "Failed to lock run-file `" << s_runFilename << '\'' << endl;

    return true;
}