
org.gnome.glib.RecMutex Maven / Gradle / Ivy
// Java-GI - Java language bindings for GObject-Introspection-based libraries
// Copyright (C) 2022-2024 Jan-Willem Harmannij
//
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see .
//
// This file has been generated with Java-GI.
// Do not edit this file directly!
// Visit for more information.
//
package org.gnome.glib;
import io.github.jwharm.javagi.base.ManagedInstance;
import io.github.jwharm.javagi.interop.Interop;
import java.lang.Deprecated;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import javax.annotation.processing.Generated;
/**
* The GRecMutex struct is an opaque data structure to represent a
* recursive mutex. It is similar to a {@code GMutex} with the difference
* that it is possible to lock a GRecMutex multiple times in the same
* thread without deadlock. When doing so, care has to be taken to
* unlock the recursive mutex as often as it has been locked.
*
* If a {@code GRecMutex} is allocated in static storage then it can be used
* without initialisation. Otherwise, you should call
* g_rec_mutex_init() on it and g_rec_mutex_clear() when done.
*
* A GRecMutex should only be accessed with the
* g_rec_mutex_ functions.
* @version 2.32
*/
@Generated("io.github.jwharm.JavaGI")
public class RecMutex extends ManagedInstance {
static {
GLib.javagi$ensureInitialized();
}
/**
* Create a RecMutex proxy instance for the provided memory address.
*
* @param address the memory address of the native object
*/
public RecMutex(MemorySegment address) {
super(Interop.reinterpret(address, getMemoryLayout().byteSize()));
}
/**
* Allocate a new RecMutex.
*
* @param arena to control the memory allocation scope
*/
public RecMutex(Arena arena) {
super(arena.allocate(getMemoryLayout()));
}
/**
* Allocate a new RecMutex.
* The memory is allocated with {@link Arena#ofAuto}.
*/
public RecMutex() {
super(Arena.ofAuto().allocate(getMemoryLayout()));
}
/**
* Allocate a new RecMutex with the fields set to the provided values.
*
* @param p value for the field {@code p}
* @param i value for the field {@code i}
* @param arena to control the memory allocation scope
*/
public RecMutex(MemorySegment p, int[] i, Arena arena) {
this(arena);
writeP(p);
writeI(i, arena);
}
/**
* Allocate a new RecMutex with the fields set to the provided values.
* The memory is allocated with {@link Arena#ofAuto}.
*
* @param p value for the field {@code p}
* @param i value for the field {@code i}
*/
public RecMutex(MemorySegment p, int[] i) {
this(Arena.ofAuto());
writeP(p);
writeI(i, Arena.ofAuto());
}
/**
* The memory layout of the native struct.
* @return the memory layout
*/
public static MemoryLayout getMemoryLayout() {
return MemoryLayout.structLayout(
ValueLayout.ADDRESS.withName("p"),
MemoryLayout.sequenceLayout(2, ValueLayout.JAVA_INT).withName("i")
).withName("GRecMutex");
}
/**
* Allocate a new RecMutex.
*
* @param arena to control the memory allocation scope
* @return a new, uninitialized {@link RecMutex}
* @deprecated Replaced by {@link RecMutex#RecMutex(Arena)}
*/
@Deprecated
public static RecMutex allocate(Arena arena) {
MemorySegment segment = arena.allocate(getMemoryLayout());
return new RecMutex(segment);
}
/**
* Allocate a new RecMutex with the fields set to the provided values.
*
* @param arena to control the memory allocation scope
* @param p value for the field {@code p}
* @param i value for the field {@code i}
* @return a new {@link RecMutex} with the fields set to the provided values
* @deprecated Replaced by {@link RecMutex#RecMutex(java.lang.foreign.MemorySegment, int[], Arena)}
*/
@Deprecated
public static RecMutex allocate(Arena arena, MemorySegment p, int[] i) {
return new RecMutex(p, i, arena);
}
/**
* Read the value of the field {@code p}.
*
* @return The value of the field {@code p}
*/
public MemorySegment readP() {
var _result = (MemorySegment) getMemoryLayout()
.varHandle(MemoryLayout.PathElement.groupElement("p")).get(handle());
return _result;
}
/**
* Write a value in the field {@code p}.
*
* @param p The new value for the field {@code p}
*/
public void writeP(MemorySegment p) {
getMemoryLayout().varHandle(MemoryLayout.PathElement.groupElement("p"))
.set(handle(), (p == null ? MemorySegment.NULL : p));
}
/**
* Read the value of the field {@code i}.
*
* @return The value of the field {@code i}
*/
public int[] readI() {
Arena _arena = Arena.ofAuto();
var _result = (MemorySegment) getMemoryLayout()
.varHandle(MemoryLayout.PathElement.groupElement("i")).get(handle());
return Interop.getIntegerArrayFrom(_result, 2, _arena, false);
}
/**
* Write a value in the field {@code i}.
*
* @param i The new value for the field {@code i}
* @param _arena to control the memory allocation scope
*/
public void writeI(int[] i, Arena _arena) {
getMemoryLayout().varHandle(MemoryLayout.PathElement.groupElement("i"))
.set(handle(), (i == null ? MemorySegment.NULL : Interop.allocateNativeArray(i, false, _arena)));
}
/**
* Frees the resources allocated to a recursive mutex with
* g_rec_mutex_init().
*
* This function should not be used with a {@code GRecMutex} that has been
* statically allocated.
*
* Calling g_rec_mutex_clear() on a locked recursive mutex leads
* to undefined behaviour.
*/
public void clear() {
try {
FunctionDescriptor _fdesc = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS);
Interop.downcallHandle("g_rec_mutex_clear", _fdesc, false).invokeExact(handle());
} catch (Throwable _err) {
throw new AssertionError("Unexpected exception occurred: ", _err);
}
}
/**
* Initializes a {@code GRecMutex} so that it can be used.
*
* This function is useful to initialize a recursive mutex
* that has been allocated on the stack, or as part of a larger
* structure.
*
* It is not necessary to initialise a recursive mutex that has been
* statically allocated.
*
{@code
* typedef struct {
* GRecMutex m;
* ...
* } Blob;
*
* Blob *b;
*
* b = g_new (Blob, 1);
* g_rec_mutex_init (&b->m);
* }
*
* Calling g_rec_mutex_init() on an already initialized {@code GRecMutex}
* leads to undefined behaviour.
*
* To undo the effect of g_rec_mutex_init() when a recursive mutex
* is no longer needed, use g_rec_mutex_clear().
*/
public void init() {
try {
FunctionDescriptor _fdesc = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS);
Interop.downcallHandle("g_rec_mutex_init", _fdesc, false).invokeExact(handle());
} catch (Throwable _err) {
throw new AssertionError("Unexpected exception occurred: ", _err);
}
}
/**
* Locks {@code rec_mutex}. If {@code rec_mutex} is already locked by another
* thread, the current thread will block until {@code rec_mutex} is
* unlocked by the other thread. If {@code rec_mutex} is already locked
* by the current thread, the 'lock count' of {@code rec_mutex} is increased.
* The mutex will only become available again when it is unlocked
* as many times as it has been locked.
*/
public void lock() {
try {
FunctionDescriptor _fdesc = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS);
Interop.downcallHandle("g_rec_mutex_lock", _fdesc, false).invokeExact(handle());
} catch (Throwable _err) {
throw new AssertionError("Unexpected exception occurred: ", _err);
}
}
/**
* Tries to lock {@code rec_mutex}. If {@code rec_mutex} is already locked
* by another thread, it immediately returns {@code false}. Otherwise
* it locks {@code rec_mutex} and returns {@code true}.
* @return {@code true} if {@code rec_mutex} could be locked
*/
public boolean trylock() {
int _result;
try {
FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.JAVA_INT,
ValueLayout.ADDRESS);
_result = (int) Interop.downcallHandle("g_rec_mutex_trylock", _fdesc, false)
.invokeExact(handle());
} catch (Throwable _err) {
throw new AssertionError("Unexpected exception occurred: ", _err);
}
return _result != 0;
}
/**
* Unlocks {@code rec_mutex}. If another thread is blocked in a
* g_rec_mutex_lock() call for {@code rec_mutex}, it will become unblocked
* and can lock {@code rec_mutex} itself.
*
* Calling g_rec_mutex_unlock() on a recursive mutex that is not
* locked by the current thread leads to undefined behaviour.
*/
public void unlock() {
try {
FunctionDescriptor _fdesc = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS);
Interop.downcallHandle("g_rec_mutex_unlock", _fdesc, false).invokeExact(handle());
} catch (Throwable _err) {
throw new AssertionError("Unexpected exception occurred: ", _err);
}
}
}