Open main menu

Sead/Locks

Lock classes that are contained in the sead namespace are classified as classes that pertain to locking regions of memory or data to prevent accidental mishandling.

CriticalSection

CriticalSection is a class that contains a region of code that is protected to ensure that it is executed on one thread at a time. To perform this, the class stores a mutex semaphore. It inherits from the IDisposer class.

namespace sead
{
    class CriticalSection : sead::IDisposer
    {
    public:
        // default constructor. Initializes the stored mutex type.
        CriticalSection();
        // constructor that initializes the section disposer with a heap and initializes the stored mutex type.
        CriticalSection(sead::Heap *);
        // constructor that initializes the section disposer with a heap and a null option, and initializes the mutex type.
        CriticalSection(sead::Heap *, sead::IDisposer::HeapNullOption);
        // destructor
        ~CriticalSection();

        // locks the section
        void lock();
        // attempts to lock the section, True is successful, False if failure
        bool tryLock();
        // unlock the section
        void unlock();

        nn::os::MutexType* mMutexType; // _20
    };
};

ReadWriteLock

The ReadWriteLock class serves as a Read/Write lock, as the name implies. The purpose of this class is to allow concurrent access for read-only processes, and requiring write operations exclusive accesses. This class is open for multiple threads to read / write data at once. It uses two semaphores for each type of lock, read and write.

namespace sead
{
    class ReadWriteLock
    {
    public:

        class SemaphoreLock : public sead::Semaphore
        {
        public:
            SemaphoreLock();

            void lock();
            void unlock();

            u32 _48;
        };

        ReadWriteLock();
        ~ReadWriteLock();

        void readLock();
        void readUnlock();
        void writeLock();
        void writeUnlock();

        u32 _0;
        u32 _4;
        sead::Semaphore mReadSemaphore; // _8
        u32 _50;
        u32 _54;
        sead::Semaphore mWriteSemaphore; // _58
        u32 _A0;
        /// Thread that is currently holding the write lock.
        sead::Thread* mWriterThread; // _A8
        u32 _B0;
    };
};

SpinLock

The SpinLock class is responsible for making threads that wish to acquire a lock be stuck in a loop until the lock is available, as it checks every iteration as it waits.

namespace sead
{
	class SpinLock
	{
	public:
        // default constructor, sets all members to 0
		SpinLock();
        // destructor
		~SpinLock();
		
        // locks the iteration
		void lock();
        // attempts to lock the iteration. True if successful, False if failure
		bool tryLock();
        // unlock the iteration
		void unlock();
		
		u64 mDest; // _0
		u32 mNumber; // _8
	};
};