Remove redundant type locking

This commit is contained in:
Jake Barnby
2026-01-21 13:59:52 +13:00
parent 802379366a
commit 124ee0df8f
+11 -43
View File
@@ -6,79 +6,47 @@ use Appwrite\GraphQL\Types\Assoc;
use Appwrite\GraphQL\Types\InputFile;
use Appwrite\GraphQL\Types\Json;
use GraphQL\Type\Definition\Type;
use Swoole\Lock;
class Types
{
private static ?Json $json = null;
private static ?Assoc $assoc = null;
private static ?InputFile $inputFile = null;
private static ?Lock $lock = null;
/**
* Get or create the shared lock for thread-safe initialization.
*/
private static function getLock(): Lock
{
if (self::$lock === null) {
self::$lock = new Lock(SWOOLE_MUTEX);
}
return self::$lock;
}
/**
* Get the JSON type (thread-safe).
* Get the JSON type.
*
* Thread-safety note: In Swoole, each worker is a separate process with its own
* static variables. Within a worker, coroutines are cooperative and only yield
* at I/O points. Since these constructors have no I/O, the null check and
* assignment execute atomically without needing locks.
*/
public static function json(): Type
{
if (self::$json === null) {
self::getLock()->lock();
try {
// Double-check after acquiring lock
if (self::$json === null) {
self::$json = new Json();
}
} finally {
self::getLock()->unlock();
}
self::$json = new Json();
}
return self::$json;
}
/**
* Get the Assoc type (thread-safe).
* Get the Assoc type.
*/
public static function assoc(): Type
{
if (self::$assoc === null) {
self::getLock()->lock();
try {
// Double-check after acquiring lock
if (self::$assoc === null) {
self::$assoc = new Assoc();
}
} finally {
self::getLock()->unlock();
}
self::$assoc = new Assoc();
}
return self::$assoc;
}
/**
* Get the InputFile type (thread-safe).
* Get the InputFile type.
*/
public static function inputFile(): Type
{
if (self::$inputFile === null) {
self::getLock()->lock();
try {
// Double-check after acquiring lock
if (self::$inputFile === null) {
self::$inputFile = new InputFile();
}
} finally {
self::getLock()->unlock();
}
self::$inputFile = new InputFile();
}
return self::$inputFile;
}