#thread safe concept

1 messages · Page 1 of 1 (latest)

magic pilot
#

never done anything like this, dont even know if this works. for context the column sizes is the size of a concrete type, user is responsible for casting a cell []u8 to a concrete type pointer

ByteTable
    rowSize: usize                        // sum of column_sizes
    rows: usize                           // how many rows exist
    capacity: usize                       // row storage capacity
    column_sizes: []usize                 // order dependant, byte size of whatever the type is
    bytes: UList(u8)                      // every component list smashed into one, order dependant, for cache efficiency
    bytes_lock: RwLock                    // a lock for anything that changes the size of bytes (addRow, remRow)
    col_locks: []RwLock                   // a lock per column
    row_locks: []RwLock                   // a lock per row

    Write to a Cell:
        if(bytes_lock == lock) wait
        if(column == locked and row == locked) wait
        else if(column == locked and row == unlocked) lock row, defer row unlock
        else if(column == unlocked and row == locked) lock column, defer column unlock
        write to cell

    init (alloc, column_sizes: []usize) ByteTable
    deinit (alloc) void

    read (column_index, row_index) []u8       
    write (alloc, column_index, row_index, cell_data: []u8) void
    addRow (alloc) row_index
    remRow (alloc, row_index)


    Iterator(alloc) ...
    Spliterator(alloc) ...
#

for mobile users

pseudo kettle
magic pilot
#

i want to store cell data in one contiguous array of bytes. the layout of the bytes is: all column 1 bytes, followed by all column 2 bytes, so on.

i want to read and write to this list of bytes from many threads safely. The idea i have is whenever i need to modify the size of the byte list i will use a bytes mutex, and whenever i need to write to a cell i check the state of the respective column mutexes which are stored in their respective lists. The idea is i can write to a cell only if the bytes mutex is not locked and both column and row mutexes are locked. if the bytes mutex is not locked and only one of the column and row mutexes are locked, i am free to write.

pseudo kettle
#

Do you ever need to read/write more than one byte at a time, atomically?

magic pilot
#

yeah, for example the first column could be for the type [3]f32 meaning each cell in the first column is comprised of 12 bytes.

pseudo kettle
#

Right, so you might need to write 3 f32s all at once, atomically?

magic pilot
#

yep

pseudo kettle
#

Okay, I would just use an RwLock on the whole datastructure

#

Your row/column mutex idea feels clever but I don't think it's sound

magic pilot
#

ideally without locking the entire column or the bytes array.
ah, yeah RwLock is ideal here

pseudo kettle
#

Keep your critical section small and you likely don't need to try and optimize to avoid locking the whole column

#

If you really do, you're deep into optimization land and I'd need more info to be able to help

magic pilot
#

this is me chasing a data architecture concept for an archetypal ECS framework. each archetype stores component data in one of these ByteTables

#

as well as a list of entity ids, shared value ids, etc

pseudo kettle
#

Gonna be honest, if you're trying to write a thread-safe ECS, you're going to need a lot of concurrency knowledge

#

It's basically database-level concurrency stuff

#

(because an ecs is basically a database lol)

magic pilot
#

true, and yeah that’s why im running with the Table, Column, Row naming. I need to build up my concurrency knowledge so im just diving into the deep end full send. how much hairline i have left once i resurface is uncertain lol

pseudo kettle
#

I'd probably recommend going on youtube and watching some database and concurrency talks :)