summaryrefslogtreecommitdiff
path: root/lib/shmem.cpp
diff options
context:
space:
mode:
authorChristoph Berg <myon@debian.org>2026-10-09 14:12:35 +0200
committerChristoph Berg <myon@debian.org>2026-10-09 14:12:35 +0200
commitffc62c792c9a5196cb4ce7a353287dfe20db6524 (patch)
tree1269efa852935b183daae4bb33a58d1b4d2ffb53 /lib/shmem.cpp
parent059831534c7e884f0a607ed86b582fd00d821731 (diff)
New upstream version 3.3.0~beta1+dfsgupstream/3.3.0_beta1+dfsg
Diffstat (limited to 'lib/shmem.cpp')
-rw-r--r--lib/shmem.cpp58
1 files changed, 45 insertions, 13 deletions
diff --git a/lib/shmem.cpp b/lib/shmem.cpp
index 7d1783d6..113babfc 100644
--- a/lib/shmem.cpp
+++ b/lib/shmem.cpp
@@ -1,18 +1,50 @@
-#include <QSharedMemory>
-#include <QLatin1String>
+#include "NativeSharedMemory.hpp"
+#include "DecoderWorkerLock.hpp"
+#include <memory>
+#include <cstdio>
+#include <exception>
-// Multiple instances: KK1D, 17 Jul 2013
-QSharedMemory shmem;
-
-struct jt9com;
+namespace
+{
+ // Release the mapping before the lifetime lock, after all Fortran cleanup.
+ std::unique_ptr<DecoderWorkerLock> worker_lock;
+ NativeSharedMemory shmem;
+}
-// C wrappers for a QSharedMemory class instance
extern "C"
{
- bool shmem_create (int nsize) {return shmem.create(nsize);}
- void shmem_setkey (char * const mykey) {shmem.setKey(QLatin1String{mykey});}
- bool shmem_attach () {return shmem.attach();}
- int shmem_size () {return static_cast<int> (shmem.size());}
- struct jt9com * shmem_address () {return reinterpret_cast<struct jt9com *>(shmem.data());}
- bool shmem_detach () {return shmem.detach();}
+ bool shmem_lock_worker (char const * path) noexcept
+ {
+ try
+ {
+ worker_lock.reset (new DecoderWorkerLock {path});
+ if (worker_lock->tryLock ()) return true;
+ std::fprintf (stderr, "Decoder worker lock: %s\n", worker_lock->errorString ().c_str ());
+ }
+ catch (...)
+ {
+ std::fputs ("Decoder worker lock: initialization failed\n", stderr);
+ }
+ return false;
+ }
+
+ bool shmem_attach (char const * name) noexcept
+ {
+ try
+ {
+ if (shmem.attach (name)) return true;
+ std::fprintf (stderr, "Decoder shared memory: %s\n", shmem.errorString ().c_str ());
+ }
+ catch (std::exception const& error)
+ {
+ std::fprintf (stderr, "Decoder shared memory: %s\n", error.what ());
+ }
+ catch (...)
+ {
+ std::fputs ("Decoder shared memory: unexpected attachment failure\n", stderr);
+ }
+ return false;
+ }
+ std::size_t shmem_size () noexcept {return shmem.size ();}
+ void * shmem_address () noexcept {return shmem.data ();}
}