The Pedigree Project  0.1
LockedFile.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "LockedFile.h"
21 #include "pedigree/kernel/process/Process.h"
22 #include "pedigree/kernel/process/Thread.h"
23 #include "pedigree/kernel/processor/Processor.h"
24 #include "pedigree/kernel/processor/ProcessorInformation.h"
25 
26 class File;
27 
29  : m_File(pFile), m_bLocked(false), m_LockerPid(0)
30 #ifdef THREADS
31  ,
32  m_Lock(false)
33 #endif
34  {};
35 
37  : m_File(0), m_bLocked(false), m_LockerPid(0)
38 #ifdef THREADS
39  ,
40  m_Lock(false)
41 #endif
42 {
43  m_File = c.m_File;
44  m_bLocked = c.m_bLocked;
46 
47 #ifdef THREADS
48  if (m_bLocked)
49  {
50  m_Lock.acquire();
51  }
52 #endif
53 }
54 
55 bool LockedFile::lock(bool bBlock)
56 {
57 #ifdef THREADS
58  if (!bBlock)
59  {
60  if (!m_Lock.tryAcquire())
61  {
62  return false;
63  }
64  }
65  else
66  m_Lock.acquire();
67 
68  // Obtained the lock
69  m_bLocked = true;
70  m_LockerPid =
71  Processor::information().getCurrentThread()->getParent()->getId();
72 #endif
73  return true;
74 }
75 
77 {
78 #ifdef THREADS
79  if (m_bLocked)
80  {
81  m_bLocked = false;
82  m_Lock.release();
83  }
84 #endif
85 }
86 
88 {
89 #ifdef THREADS
90  // If we're locked, and we aren't the locking process, we can't access the
91  // file Otherwise, the file is accessible
92  if (m_bLocked == true &&
93  Processor::information().getCurrentThread()->getParent()->getId() !=
95  return 0;
96  else
97 #endif
98  return m_File;
99 }
100 
102 {
103 #ifdef THREADS
104  if (m_bLocked)
105  return m_LockerPid;
106  else
107 #endif
108  return 0;
109 }
bool acquire(size_t n=1, size_t timeoutSecs=0, size_t timeoutUsecs=0)
Definition: Semaphore.h:62
File * m_File
Definition: LockedFile.h:67
static ProcessorInformation & information()
Definition: Processor.cc:45
size_t getLocker()
Definition: LockedFile.cc:101
size_t m_LockerPid
Definition: LockedFile.h:73
File * getFile()
Definition: LockedFile.cc:87
bool lock(bool bBlock=false)
Definition: LockedFile.cc:55
Mutex m_Lock
Definition: LockedFile.h:77
void release(size_t n=1)
Definition: Semaphore.cc:239
bool m_bLocked
Definition: LockedFile.h:70
bool tryAcquire(size_t n=1)
Definition: Semaphore.cc:223
void unlock()
Definition: LockedFile.cc:76
Definition: File.h:66