Xmipp  v3.23.11-Nereus
Public Member Functions | Public Attributes | Protected Attributes | List of all members
MpiFileMutex Class Reference

#include <xmipp_mpi.h>

Inheritance diagram for MpiFileMutex:
Inheritance graph
[legend]
Collaboration diagram for MpiFileMutex:
Collaboration graph
[legend]

Public Member Functions

 MpiFileMutex (const std::shared_ptr< MpiNode > &node)
 
 ~MpiFileMutex ()
 
void lock ()
 
void unlock ()
 
- Public Member Functions inherited from Mutex
 Mutex ()
 
virtual ~Mutex ()
 

Public Attributes

char lockFilename [L_tmpnam]
 

Protected Attributes

std::shared_ptr< MpiNodenode
 
int lockFile
 
bool fileCreator
 

Detailed Description

Mutex on files. This class extends threads mutex to also provide file locking.

Definition at line 118 of file xmipp_mpi.h.

Constructor & Destructor Documentation

◆ MpiFileMutex()

MpiFileMutex::MpiFileMutex ( const std::shared_ptr< MpiNode > &  node)

Default constructor.

Definition at line 91 of file xmipp_mpi.cpp.

92 {
93  fileCreator = false;
94 
95  if (node == nullptr || node->isMaster())
96  {
97  fileCreator = true;
98  strcpy(lockFilename, "pijol_XXXXXX");
99  if ((lockFile = mkstemp(lockFilename)) == -1)
100  {
101  perror("MpiFileMutex::Error generating tmp lock file");
102  exit(1);
103  }
104  close(lockFile);
105  }
106  //if using mpi broadcast the filename from master to slaves
107  if (node != nullptr)
108  MPI_Bcast(lockFilename, L_tmpnam, MPI_CHAR, 0, MPI_COMM_WORLD);
109 
110  if ((lockFile = open(lockFilename, O_RDWR)) == -1)
111  {
112  perror("MpiFileMutex: Error opening lock file");
113  exit(1);
114  }
115 }
char lockFilename[L_tmpnam]
Definition: xmipp_mpi.h:144
std::shared_ptr< MpiNode > node
Definition: xmipp_mpi.h:121
bool fileCreator
Definition: xmipp_mpi.h:123

◆ ~MpiFileMutex()

MpiFileMutex::~MpiFileMutex ( )

Destructor.

Definition at line 133 of file xmipp_mpi.cpp.

134 {
135  close(lockFile);
136  if (fileCreator && remove(lockFilename) == -1)
137  {
138  perror("~MpiFileMutex: error deleting lock file");
139  exit(1);
140  }
141 }
char lockFilename[L_tmpnam]
Definition: xmipp_mpi.h:144
bool fileCreator
Definition: xmipp_mpi.h:123

Member Function Documentation

◆ lock()

void MpiFileMutex::lock ( )
virtual

Function to get the access to the mutex. If the some thread has the mutex and other ask to lock will be waiting until the first one release the mutex

Reimplemented from Mutex.

Definition at line 117 of file xmipp_mpi.cpp.

118 {
119  Mutex::lock();
120  lseek(lockFile, 0, SEEK_SET);
121  if (lockf(lockFile, F_LOCK, 0)==-1)
122  REPORT_ERROR(ERR_IO_NOPERM,"Cannot lock file");
123 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Insufficient permissions to perform operation.
Definition: xmipp_error.h:138
virtual void lock()

◆ unlock()

void MpiFileMutex::unlock ( )
virtual

Function to release the mutex. This allow the access to the mutex to other threads that are waiting for it.

Reimplemented from Mutex.

Definition at line 125 of file xmipp_mpi.cpp.

126 {
127  lseek(lockFile, 0, SEEK_SET);
128  if (lockf(lockFile, F_ULOCK, 0)==-1)
129  REPORT_ERROR(ERR_IO_NOPERM,"Cannot unlock file");
130  Mutex::unlock();
131 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
virtual void unlock()
Insufficient permissions to perform operation.
Definition: xmipp_error.h:138

Member Data Documentation

◆ fileCreator

bool MpiFileMutex::fileCreator
protected

Definition at line 123 of file xmipp_mpi.h.

◆ lockFile

int MpiFileMutex::lockFile
protected

Definition at line 122 of file xmipp_mpi.h.

◆ lockFilename

char MpiFileMutex::lockFilename[L_tmpnam]

Definition at line 144 of file xmipp_mpi.h.

◆ node

std::shared_ptr<MpiNode> MpiFileMutex::node
protected

Definition at line 121 of file xmipp_mpi.h.


The documentation for this class was generated from the following files: