MPD: lib/nfs/Blocking.hxx Source File

MPD 0.20.15

Blocking.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_BLOCKING_NFS_CALLBACK_HXX
21 #define MPD_BLOCKING_NFS_CALLBACK_HXX
22
23 #include "check.h"
24 #include "Callback.hxx"
25 #include "Lease.hxx"
26 #include "thread/Mutex.hxx"
27 #include "thread/Cond.hxx"
28
29 #include <exception>
30
31 class NfsConnection;
32
38 class BlockingNfsOperation : protected NfsCallback, NfsLease {
39 static constexpr std::chrono::steady_clock::duration timeout =
40 std::chrono::minutes(1);
41
42 Mutex mutex;
43 Cond cond;
44
45 bool finished;
46
47 std::exception_ptr error;
48
49 protected:
50 NfsConnection &connection;
51
52 public:
53 BlockingNfsOperation(NfsConnection &_connection)
54 :finished(false), connection(_connection) {}
55
59 void Run();
60
61 private:
62 bool LockWaitFinished() {
63 const std::lock_guard<Mutex> protect(mutex);
64 while (!finished)
65 if (!cond.timed_wait(mutex, timeout))
66 return false;
67
68 return true;
69 }
70
75 void LockSetFinished() {
76 const std::lock_guard<Mutex> protect(mutex);
77 finished = true;
78 cond.signal();
79 }
80
81 /* virtual methods from NfsLease */
82 void OnNfsConnectionReady() final;
83 void OnNfsConnectionFailed(std::exception_ptr e) final;
84 void OnNfsConnectionDisconnected(std::exception_ptr e) final;
85
86 /* virtual methods from NfsCallback */
87 void OnNfsCallback(unsigned status, void *data) final;
88 void OnNfsError(std::exception_ptr &&e) final;
89
90 protected:
91 virtual void Start() = 0;
92 virtual void HandleResult(unsigned status, void *data) = 0;
93 };
94
95 #endif
NfsLease
Definition: Lease.hxx:27
check.h
PosixCond::timed_wait
bool timed_wait(PosixMutex &mutex, std::chrono::steady_clock::duration timeout)
Definition: PosixCond.hxx:95
Cond
Definition: Cond.hxx:41
Mutex
Definition: Mutex.hxx:43
BlockingNfsOperation::HandleResult
virtual void HandleResult(unsigned status, void *data)=0
Cond.hxx
BlockingNfsOperation::Run
void Run()
Throws std::runtime_error on error.
BlockingNfsOperation
Utility class to implement a blocking NFS call using the libnfs async API.
Definition: Blocking.hxx:38
BlockingNfsOperation::Start
virtual void Start()=0
NfsConnection
An asynchronous connection to a NFS server.
Definition: Connection.hxx:42
Mutex.hxx
BlockingNfsOperation::connection
NfsConnection & connection
Definition: Blocking.hxx:50
NfsCallback
Callbacks for an asynchronous libnfs operation.
Definition: Callback.hxx:32
PosixCond::signal
void signal()
Definition: PosixCond.hxx:65
Callback.hxx
BlockingNfsOperation::BlockingNfsOperation
BlockingNfsOperation(NfsConnection &_connection)
Definition: Blocking.hxx:53
e
int e
Definition: Log.hxx:115
Lease.hxx