[][src]Struct qt_core::QSystemSemaphore

#[repr(C)]
pub struct QSystemSemaphore { /* fields omitted */ }

The QSystemSemaphore class provides a general counting system semaphore.

C++ class: QSystemSemaphore.

C++ documentation:

The QSystemSemaphore class provides a general counting system semaphore.

A semaphore is a generalization of a mutex. While a mutex can be locked only once, a semaphore can be acquired multiple times. Typically, a semaphore is used to protect a certain number of identical resources.

Like its lighter counterpart QSemaphore, a QSystemSemaphore can be accessed from multiple threads. Unlike QSemaphore, a QSystemSemaphore can also be accessed from multiple processes. This means QSystemSemaphore is a much heavier class, so if your application doesn't need to access your semaphores across multiple processes, you will probably want to use QSemaphore.

Semaphores support two fundamental operations, acquire() and release():

acquire() tries to acquire one resource. If there isn't a resource available, the call blocks until a resource becomes available. Then the resource is acquired and the call returns.

release() releases one resource so it can be acquired by another process. The function can also be called with a parameter n > 1, which releases n resources.

A system semaphore is created with a string key that other processes can use to use the same semaphore.

Example: Create a system semaphore

QSystemSemaphore sem("market", 3, QSystemSemaphore::Create); // resources available == 3 sem.acquire(); // resources available == 2 sem.acquire(); // resources available == 1 sem.acquire(); // resources available == 0 sem.release(); // resources available == 1 sem.release(2); // resources available == 3

A typical application of system semaphores is for controlling access to a circular buffer shared by a producer process and a consumer processes.

Methods

impl QSystemSemaphore[src]

pub unsafe fn acquire(&mut self) -> bool[src]

Acquires one of the resources guarded by this semaphore, if there is one available, and returns true. If all the resources guarded by this semaphore have already been acquired, the call blocks until one of them is released by another process or thread having a semaphore with the same key.

Calls C++ function: bool QSystemSemaphore::acquire().

C++ documentation:

Acquires one of the resources guarded by this semaphore, if there is one available, and returns true. If all the resources guarded by this semaphore have already been acquired, the call blocks until one of them is released by another process or thread having a semaphore with the same key.

If false is returned, a system error has occurred. Call error() to get a value of QSystemSemaphore::SystemSemaphoreError that indicates which error occurred.

See also release().

pub unsafe fn error(&self) -> SystemSemaphoreError[src]

Returns a value indicating whether an error occurred, and, if so, which error it was.

Calls C++ function: QSystemSemaphore::SystemSemaphoreError QSystemSemaphore::error() const.

C++ documentation:

Returns a value indicating whether an error occurred, and, if so, which error it was.

See also errorString().

pub unsafe fn error_string(&self) -> CppBox<QString>[src]

Returns a text description of the last error that occurred. If error() returns an error value, call this function to get a text string that describes the error.

Calls C++ function: QString QSystemSemaphore::errorString() const.

C++ documentation:

Returns a text description of the last error that occurred. If error() returns an error value, call this function to get a text string that describes the error.

See also error().

pub unsafe fn key(&self) -> CppBox<QString>[src]

Returns the key assigned to this system semaphore. The key is the name by which the semaphore can be accessed from other processes.

Calls C++ function: QString QSystemSemaphore::key() const.

C++ documentation:

Returns the key assigned to this system semaphore. The key is the name by which the semaphore can be accessed from other processes.

See also setKey().

pub unsafe fn new_3a(
    key: impl CastInto<Ref<QString>>,
    initial_value: c_int,
    mode: AccessMode
) -> CppBox<QSystemSemaphore>
[src]

Requests a system semaphore for the specified key. The parameters initialValue and mode are used according to the following rules, which are system dependent.

Calls C++ function: [constructor] void QSystemSemaphore::QSystemSemaphore(const QString& key, int initialValue = …, QSystemSemaphore::AccessMode mode = …).

C++ documentation:

Requests a system semaphore for the specified key. The parameters initialValue and mode are used according to the following rules, which are system dependent.

In Unix, if the mode is Open and the system already has a semaphore identified by key, that semaphore is used, and the semaphore's resource count is not changed, i.e., initialValue is ignored. But if the system does not already have a semaphore identified by key, it creates a new semaphore for that key and sets its resource count to initialValue.

In Unix, if the mode is Create and the system already has a semaphore identified by key, that semaphore is used, and its resource count is set to initialValue. If the system does not already have a semaphore identified by key, it creates a new semaphore for that key and sets its resource count to initialValue.

In Windows, mode is ignored, and the system always tries to create a semaphore for the specified key. If the system does not already have a semaphore identified as key, it creates the semaphore and sets its resource count to initialValue. But if the system already has a semaphore identified as key it uses that semaphore and ignores initialValue.

The mode parameter is only used in Unix systems to handle the case where a semaphore survives a process crash. In that case, the next process to allocate a semaphore with the same key will get the semaphore that survived the crash, and unless mode is Create, the resource count will not be reset to initialValue but will retain the initial value it had been given by the crashed process.

See also acquire() and key().

pub unsafe fn new_2a(
    key: impl CastInto<Ref<QString>>,
    initial_value: c_int
) -> CppBox<QSystemSemaphore>
[src]

Requests a system semaphore for the specified key. The parameters initialValue and mode are used according to the following rules, which are system dependent.

Calls C++ function: [constructor] void QSystemSemaphore::QSystemSemaphore(const QString& key, int initialValue = …).

C++ documentation:

Requests a system semaphore for the specified key. The parameters initialValue and mode are used according to the following rules, which are system dependent.

In Unix, if the mode is Open and the system already has a semaphore identified by key, that semaphore is used, and the semaphore's resource count is not changed, i.e., initialValue is ignored. But if the system does not already have a semaphore identified by key, it creates a new semaphore for that key and sets its resource count to initialValue.

In Unix, if the mode is Create and the system already has a semaphore identified by key, that semaphore is used, and its resource count is set to initialValue. If the system does not already have a semaphore identified by key, it creates a new semaphore for that key and sets its resource count to initialValue.

In Windows, mode is ignored, and the system always tries to create a semaphore for the specified key. If the system does not already have a semaphore identified as key, it creates the semaphore and sets its resource count to initialValue. But if the system already has a semaphore identified as key it uses that semaphore and ignores initialValue.

The mode parameter is only used in Unix systems to handle the case where a semaphore survives a process crash. In that case, the next process to allocate a semaphore with the same key will get the semaphore that survived the crash, and unless mode is Create, the resource count will not be reset to initialValue but will retain the initial value it had been given by the crashed process.

See also acquire() and key().

pub unsafe fn new_1a(
    key: impl CastInto<Ref<QString>>
) -> CppBox<QSystemSemaphore>
[src]

Requests a system semaphore for the specified key. The parameters initialValue and mode are used according to the following rules, which are system dependent.

Calls C++ function: [constructor] void QSystemSemaphore::QSystemSemaphore(const QString& key).

C++ documentation:

Requests a system semaphore for the specified key. The parameters initialValue and mode are used according to the following rules, which are system dependent.

In Unix, if the mode is Open and the system already has a semaphore identified by key, that semaphore is used, and the semaphore's resource count is not changed, i.e., initialValue is ignored. But if the system does not already have a semaphore identified by key, it creates a new semaphore for that key and sets its resource count to initialValue.

In Unix, if the mode is Create and the system already has a semaphore identified by key, that semaphore is used, and its resource count is set to initialValue. If the system does not already have a semaphore identified by key, it creates a new semaphore for that key and sets its resource count to initialValue.

In Windows, mode is ignored, and the system always tries to create a semaphore for the specified key. If the system does not already have a semaphore identified as key, it creates the semaphore and sets its resource count to initialValue. But if the system already has a semaphore identified as key it uses that semaphore and ignores initialValue.

The mode parameter is only used in Unix systems to handle the case where a semaphore survives a process crash. In that case, the next process to allocate a semaphore with the same key will get the semaphore that survived the crash, and unless mode is Create, the resource count will not be reset to initialValue but will retain the initial value it had been given by the crashed process.

See also acquire() and key().

pub unsafe fn release_1a(&mut self, n: c_int) -> bool[src]

Releases n resources guarded by the semaphore. Returns true unless there is a system error.

Calls C++ function: bool QSystemSemaphore::release(int n = …).

C++ documentation:

Releases n resources guarded by the semaphore. Returns true unless there is a system error.

Example: Create a system semaphore having five resources; acquire them all and then release them all.

QSystemSemaphore sem("market", 5, QSystemSemaphore::Create); for (int i = 0; i < 5; ++i) // acquire all 5 resources sem.acquire(); sem.release(5); // release the 5 resources

This function can also "create" resources. For example, immediately following the sequence of statements above, suppose we add the statement:

sem.release(10); // "create" 10 new resources

Ten new resources are now guarded by the semaphore, in addition to the five that already existed. You would not normally use this function to create more resources.

See also acquire().

pub unsafe fn release_0a(&mut self) -> bool[src]

Releases n resources guarded by the semaphore. Returns true unless there is a system error.

Calls C++ function: bool QSystemSemaphore::release().

C++ documentation:

Releases n resources guarded by the semaphore. Returns true unless there is a system error.

Example: Create a system semaphore having five resources; acquire them all and then release them all.

QSystemSemaphore sem("market", 5, QSystemSemaphore::Create); for (int i = 0; i < 5; ++i) // acquire all 5 resources sem.acquire(); sem.release(5); // release the 5 resources

This function can also "create" resources. For example, immediately following the sequence of statements above, suppose we add the statement:

sem.release(10); // "create" 10 new resources

Ten new resources are now guarded by the semaphore, in addition to the five that already existed. You would not normally use this function to create more resources.

See also acquire().

pub unsafe fn set_key_3a(
    &mut self,
    key: impl CastInto<Ref<QString>>,
    initial_value: c_int,
    mode: AccessMode
)
[src]

This function works the same as the constructor. It reconstructs this QSystemSemaphore object. If the new key is different from the old key, calling this function is like calling the destructor of the semaphore with the old key, then calling the constructor to create a new semaphore with the new key. The initialValue and mode parameters are as defined for the constructor.

Calls C++ function: void QSystemSemaphore::setKey(const QString& key, int initialValue = …, QSystemSemaphore::AccessMode mode = …).

C++ documentation:

This function works the same as the constructor. It reconstructs this QSystemSemaphore object. If the new key is different from the old key, calling this function is like calling the destructor of the semaphore with the old key, then calling the constructor to create a new semaphore with the new key. The initialValue and mode parameters are as defined for the constructor.

See also QSystemSemaphore() and key().

pub unsafe fn set_key_2a(
    &mut self,
    key: impl CastInto<Ref<QString>>,
    initial_value: c_int
)
[src]

This function works the same as the constructor. It reconstructs this QSystemSemaphore object. If the new key is different from the old key, calling this function is like calling the destructor of the semaphore with the old key, then calling the constructor to create a new semaphore with the new key. The initialValue and mode parameters are as defined for the constructor.

Calls C++ function: void QSystemSemaphore::setKey(const QString& key, int initialValue = …).

C++ documentation:

This function works the same as the constructor. It reconstructs this QSystemSemaphore object. If the new key is different from the old key, calling this function is like calling the destructor of the semaphore with the old key, then calling the constructor to create a new semaphore with the new key. The initialValue and mode parameters are as defined for the constructor.

See also QSystemSemaphore() and key().

pub unsafe fn set_key_1a(&mut self, key: impl CastInto<Ref<QString>>)[src]

This function works the same as the constructor. It reconstructs this QSystemSemaphore object. If the new key is different from the old key, calling this function is like calling the destructor of the semaphore with the old key, then calling the constructor to create a new semaphore with the new key. The initialValue and mode parameters are as defined for the constructor.

Calls C++ function: void QSystemSemaphore::setKey(const QString& key).

C++ documentation:

This function works the same as the constructor. It reconstructs this QSystemSemaphore object. If the new key is different from the old key, calling this function is like calling the destructor of the semaphore with the old key, then calling the constructor to create a new semaphore with the new key. The initialValue and mode parameters are as defined for the constructor.

See also QSystemSemaphore() and key().

Trait Implementations

impl CppDeletable for QSystemSemaphore[src]

unsafe fn delete(&mut self)[src]

The destructor destroys the QSystemSemaphore object, but the underlying system semaphore is not removed from the system unless this instance of QSystemSemaphore is the last one existing for that system semaphore.

Calls C++ function: [destructor] void QSystemSemaphore::~QSystemSemaphore().

C++ documentation:

The destructor destroys the QSystemSemaphore object, but the underlying system semaphore is not removed from the system unless this instance of QSystemSemaphore is the last one existing for that system semaphore.

Two important side effects of the destructor depend on the system. In Windows, if acquire() has been called for this semaphore but not release(), release() will not be called by the destructor, nor will the resource be released when the process exits normally. This would be a program bug which could be the cause of a deadlock in another process trying to acquire the same resource. In Unix, acquired resources that are not released before the destructor is called are automatically released when the process exits.

Auto Trait Implementations

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> StaticUpcast<T> for T[src]

impl<T, U> CastInto<U> for T where
    U: CastFrom<T>, 
[src]