/** @file thread.c  Thread object.

@authors Copyright (c) 2017-2026 Jaakko Keränen <jaakko.keranen@iki.fi>

@par License

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

<small>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</small>
*/

#include "the_Foundation/thread.h"
#include "the_Foundation/mutex.h"

/* garbage.c */
void deinitForThread_Garbage_(void);

/*-------------------------------------------------------------------------------------*/

static tss_t currentThread_; /* thread-local pointer to the current iThread */
static iBool inited_Threads_;

void deinit_Threads_(void) {
    tss_delete(currentThread_);
    inited_Threads_ = iFalse;
}

static void init_Threads_(void) {
    if (!inited_Threads_) {
        tss_create(&currentThread_, NULL);
        inited_Threads_ = iTrue;
    }
}

void init_Threads(void) {
    init_Threads_();
}

void finish_Thread_(iThread *d) { /* called from threadpool.c as well */
    iGuardMutex(&d->mutex, {
        d->state = finished_ThreadState;
        signalAll_Condition(&d->finishedCond);
    });
    iNotifyAudience(d, finished, ThreadFinished);
    iRecycle();
}

static int run_Threads_(void *arg) {
    iThread *d = (iThread *) arg;
    if (!isEmpty_String(&d->name)) {
#if defined (iPlatformApple)
        pthread_setname_np(cstr_String(&d->name));
#endif
#if defined (iPlatformLinux)
        pthread_setname_np(d->id, cstr_String(&d->name));
#endif
    }
    if (d->flags & terminationEnabled_ThreadFlag) {
#if defined (iHavePThreadCancel)
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
#endif
    }
    tss_set(currentThread_, d);
    d->result = d->run(d);
    tss_set(currentThread_, NULL);
    /* Notify observers that the thread is done. */
    finish_Thread_(d);
    deref_Object(d);
    deinitForThread_Garbage_();
    thrd_exit(0); /* thread-local data gets deleted */
#if !defined(_MSC_VER) /* unreachable code */
    return 0;
#endif
}

/*-------------------------------------------------------------------------------------*/

iDefineClass(Thread)
iDefineObjectConstructionArgs(Thread, (iThreadRunFunc run), run)

void init_Thread(iThread *d, iThreadRunFunc run) {
    init_Mutex(&d->mutex);
    init_Condition(&d->finishedCond);
    init_String(&d->name);
    d->result = 0;
    d->run = run;
#if !defined(_MSC_VER)
    d->id = 0;
#endif
    d->flags = 0;
    d->userData = NULL;
    d->state = created_ThreadState;
    d->finished = NULL;
}

void deinit_Thread(iThread *d) {
    iAssert(d->state != running_ThreadState);
    if (d->state == running_ThreadState) {
        iWarning("[Thread] thread %p is being destroyed while still running\n", d);
    }
    delete_Audience(d->finished);
    deinit_Condition(&d->finishedCond);
    deinit_Mutex(&d->mutex);
    deinit_String(&d->name);
}

void start_Thread(iThread *d) {
    iGuardMutex(&d->mutex, {
        iAssert(d->state == created_ThreadState);
        d->state = running_ThreadState;
        ref_Object(d); /* hold a ref until the thread finishes (released in run_Threads_) */
        thrd_create(&d->id, run_Threads_, d);
        iDebug("[Thread] created thread ID %p (%s)\n", d->id, cstr_String(&d->name));
    });
}

void setName_Thread(iThread *d, const char *name) {
    iAssert(d->state == created_ThreadState);
    setCStr_String(&d->name, name);
}

void setUserData_Thread(iThread *d, void *userData) {
    iGuardMutex(&d->mutex, d->userData = userData);
}

void setTerminationEnabled_Thread(iThread *d, iBool enable) {
    iChangeFlags(d->flags, terminationEnabled_ThreadFlag, enable);
}

iBool isRunning_Thread(const iThread *d) {
    iBool ret;
    iGuardMutex(&d->mutex, ret = (d->state == running_ThreadState));
    return ret;
}

iBool isFinished_Thread(const iThread *d) {
    iBool ret;
    iGuardMutex(&d->mutex, ret = (d->state == finished_ThreadState));
    return ret;
}

const iString *name_Thread(const iThread *d) {
    if (!d) {
        static iString *name_;
        if (!name_) {
            name_ = newCStr_String("main thread");
        }
        return name_;
    }
    return &d->name;
}

void *userData_Thread(const iThread *d) {
    return d->userData;
}

iThreadResult result_Thread(const iThread *d) {
    join_Thread(iConstCast(iThread *, d));
    iAssert(d->state == finished_ThreadState);
    return d->result;
}

void join_Thread(iThread *d) {
    if (!d) return;
#if !defined (_MSC_VER)
    iAssert(d->id != thrd_current());
#endif
    if (isCurrent_Thread(d)) return;
    lock_Mutex(&d->mutex);
    if (d->state == running_ThreadState) {
        wait_Condition(&d->finishedCond, &d->mutex);
    }
    unlock_Mutex(&d->mutex);
    thrd_join(d->id, NULL);
}

void terminate_Thread(iThread *d) {
    iAssert(d->flags & terminationEnabled_ThreadFlag);
#if defined (iHavePThreadCancel)
    pthread_cancel(d->id);
#else
    iDebugOnly(d);
#endif
}

void sleep_Thread(double seconds) {
    iTime dur;
    initSeconds_Time(&dur, seconds);
    thrd_sleep(&dur.ts, NULL);
}

iThread *current_Thread(void) {
    if (!inited_Threads_) return NULL;
    return tss_get(currentThread_);
}

iBool isCurrent_Thread(const iThread *d) {
    iAssert(d);
    return thrd_equal(d->id, thrd_current()) != 0;
}
