#Does this use of AtomicU8 makes sense? It is supposed to imitate a state machine that queues a job

6 messages · Page 1 of 1 (latest)

candid quiver
#

specifically, i have one thread that will submit commands like this

    // Means there are no other reload config job being queued
    if self
        .config_mutex
        .compare_exchange(0, 1, Ordering::Relaxed, Ordering::Relaxed)
        .is_ok()
    {
        let _ = self.event_sink.submit_command(
            LAPCE_UI_COMMAND,
            LapceUICommand::ReloadConfig,
            Target::Auto,
        );
    } else {
        // If not, just try to increment by 1 if and only if there's only 1 other job
        let _ = self
            .config_mutex
            .compare_exchange(1, 2, Ordering::Relaxed, Ordering::Relaxed);
    }
#

and then another thread that takes in command

// Means that someone else queued for another job.
                        // Submit the job for next time
                        if data
                            .config_mutex
                            .compare_exchange(
                                2,
                                1,
                                std::sync::atomic::Ordering::Relaxed,
                                std::sync::atomic::Ordering::Relaxed,
                            )
                            .is_ok()
                        {
                            ctx.submit_command(Command::new(
                                LAPCE_UI_COMMAND,
                                LapceUICommand::ReloadConfig,
                                Target::Auto,
                            ));
                        } else {
                            // Means that there are no other reload config job
                            // Reset to default initial state of 0
                            let _ = data.config_mutex.compare_exchange(
                                1,
                                0,
                                std::sync::atomic::Ordering::Relaxed,
                                std::sync::atomic::Ordering::Relaxed,
                            );
                        }
radiant quiver
#

I'm not sure I understand the control flow here. anyway, using relaxed ordering means no happens-before relations are implied between atomic operations in different threads. so I don't think that's actually acting like a mutex?

open current
#

Yeah, it definitely won't act like a mutex

#

If you don't understand orderings, head to #932329954492948601 to ask about them, or alternatively save time and effort and use a mutex

blazing spire