render_threads.c
author Tero Marttila <terom@fixme.fi>
Wed, 27 Aug 2008 21:30:32 +0300
changeset 41 540737bf6bac
parent 23 31307efd7e78
permissions -rw-r--r--
sending requests, and partial support for receiving -- incomplete, not tested
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include <stdlib.h>
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
#include <assert.h>
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
#include <pthread.h>
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
23
31307efd7e78 new render_threads module, make node_main use it, compiles, not yet tested
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
     6
//#define DEBUG_ENABLED
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
     7
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
#include "common.h"
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
#include "render_threads.h"
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
#include "render_slices_struct.h"
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
#include "render_mandelbrot.h"
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
/*
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
 * Render a mandelbrot in several slices simultaneously using threads. 
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
 *
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
 * This works as an n:1 producer-consumer model, where we have n worker threads
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
 * rendering n slices, and one manager thread that processes the completed rows.
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
 * 
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
 * The worker threads function by first rendering a row of pixel data into memory,
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
 * and then calling a function that will do something with that completed segment.
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
 *  
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
 *  * clear the can_continue marker, because the real value may change depending
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
 *    on what our segment does, and we don't want stale values from before we
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
 *    processed thse segment in it
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
 *      * process the segment
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
 *
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
 *  * if the segment didn't complete a row, wait until some worker thread submits
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
 *    a segment that does, or the manager thread processes a row
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
 *      * if noone has signaled the cond in between us deciding what to do with
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
 *        the segment and this step,
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
 *          * wait on the cond
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
 *      * we can proceed to render the next segment
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
 *          * we should now be marked as continueable
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
 *
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
 *  * if the segment completed a row, the manager thread needs to process this
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
 *    newly completed row. 
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
 *      * if we can also continue on to the next row without waiting for the manager
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
 *        to process the row we just filled
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
 *          * mark all the segments as continueable
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
 *          * signal the cond the threads may be waiting on
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
 *      * increment the waiting_rows counter
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
 *          * signal the manager in case it's waiting
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
 *      * else, if noone has signaled the continue cond after we processed the
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
 *        segment,
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
 *          * wait on the cond
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
 *      * we can proceed to render the next segment
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
 *          * we should now be marked as continueable
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
 *
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
 * * if we have no more segments to submit
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
 *      * update the slices_running counter
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
 *          * if the counter is now zero (we were the last slice to complete)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
 *              * signal the manager thread that it doesn't need to wait anymore
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
 *
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
 * The manager thread just sits in a loop calling process_row until all the slices
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
 * have completed.
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
 *
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
 * * if no workers have updated the waiting_rows counter since we last processed
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
 *   a row, and the slices_running counter isn't zero (we still have threads
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
 *   running)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
 *      * wait on a cond until something happens
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
 *
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
 * * if waiting_rows is nonzero
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
 *      * decrement it
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
 *      * process a row
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
 *      * if processing the row means that waiting slices can continue
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
 *          * mark all the segments as continueable
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
 *          * signal the cond the threads may be waiting on
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
 *
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
 * * if slices_running is zero
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
 *      * break out of the loop
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
 *
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
 * * otherwise, go back to the start again
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
 */     
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
struct render_threads {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
    // do I need to free this?
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
    int owned_by_me;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
    // info for the induvidual threads
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
    struct render_thread {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
        // the slice that we need to render
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
        struct render_slice_info *slice_info;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
        // our thread id
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
        pthread_t thread;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
        // how many times we are allowed to continue now
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
        int can_continue;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
        // how many segments we've written
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
        int segments_done;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
        // ptr back to ctx
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
        struct render_threads *self;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
    } threads[RENDER_SLICES_MAX];
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
    // the render_slices struct
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
    struct render_slices slices_info;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
    // the manager thread id
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
    pthread_t manager_thread;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
    // how many slices?
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
    int slice_count;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
    /*
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
     * The worker threads must be careful to wait for the manager thread to start running before they attempt to signal
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
     * the process_row_cond. This is done via this flag, in conjunction with continue_{cond,mut}. 
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
     *
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
     * Workers should lock continue_mut, and if this flag is zero, wait on continue_cond (releasing the mutex). If the
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
     * flag is already nonzero, the worker threads do not need to cond_wait and can just continue.
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
     *
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
     * On startup, the manager thread will lock continue_mut, set this to nonzero, signal continue_cond, and unlock
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
     * continue_mut as normal.
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
     */
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
    int manager_running;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   119
    /*
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   120
     * Signals to the manager
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
     */
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   122
    int slices_running;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   123
    int waiting_rows;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   124
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
    /* the inter-thread communication stuff */
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   126
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   127
    // the worker threads signal this when their segment_done call indicates that the manager thread should call
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   128
    // process_row now.
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   129
    pthread_cond_t process_row_cond;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   130
    pthread_mutex_t process_row_mut;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   131
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
    // the worker threads and the manager thread signal this when segment_done or process_row indicate that the worker
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   133
    // threads should call segment_done again. Worker threads that receive ~SLICE_CONTINUE from segment_done will wait
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   134
    // on this cond.
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   135
    pthread_cond_t continue_cond;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   136
    pthread_mutex_t continue_mut;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   137
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   138
    // used to protect slices_info
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
    pthread_mutex_t slices_mut;
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   140
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   141
    /*
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   142
     * Return value from the manager thread
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   143
     */
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   144
    enum render_threads_retval {
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   145
        RETVAL_SUCCESS,
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   146
        RETVAL_FAILURE,
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   147
    } manager_retval;
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   148
};
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   149
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   150
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   151
void render_threads_deinit (struct render_threads *ctx) {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   152
    render_slices_deinit(&ctx->slices_info);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   153
}
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   154
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   155
void render_threads_free (struct render_threads *ctx) {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   156
    render_threads_deinit(ctx);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   157
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   158
    if (ctx->owned_by_me)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   159
        free(ctx);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   160
}
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   161
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   162
int _render_threads_slice_row (void *arg, unsigned char *rowbuf) {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   163
    struct render_thread *subctx = arg;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   164
    int status, i;
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   165
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   166
    // test for cancelation, we don't hold any locks right now
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   167
    pthread_testcancel();
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   168
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   169
    // we need to handle the render_slices state atomically, so lock it
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   170
    pthread_mutex_lock(&subctx->self->slices_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   171
    pthread_mutex_lock(&subctx->self->continue_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   172
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   173
    // make sure that we don't have any stale state in this
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   174
    subctx->can_continue = 0;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   175
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   176
    // process the segment
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   177
    status = render_slices_segment_done(&subctx->self->slices_info, subctx->slice_info->index);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   178
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   179
    // debugging
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   180
    DEBUG("slice %zu: segment_done, status=(%s %s), row=%d", subctx->slice_info->index,
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   181
        status & SLICE_PROCESS_ROW ? "SLICE_PROCESS_ROW" : "", 
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   182
        status & SLICE_CONTINUE ? "SLICE_CONTINUE" : "",
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   183
        ++subctx->segments_done
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   184
    );
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   185
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   186
    // do we need to notify the other worker threads?
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   187
    if (status & SLICE_CONTINUE) {
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   188
        DEBUG("slice %zu: signal continue", subctx->slice_info->index);
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   189
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   190
        // mark them as continueable
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   191
        for (i = 0; i < subctx->self->slice_count; i++)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   192
            subctx->self->threads[i].can_continue = 1;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   193
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   194
        // signal then to continue
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   195
        pthread_cond_broadcast(&subctx->self->continue_cond);
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   196
    }
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   197
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   198
    // done with the slices stuff
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   199
    pthread_mutex_unlock(&subctx->self->continue_mut);
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   200
    pthread_mutex_unlock(&subctx->self->slices_mut);
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   201
   
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   202
    // tell the manager thread that it can process a row
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   203
    if (status & SLICE_PROCESS_ROW) {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   204
        // grab the process_row lock so that we can do stuff with it
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   205
        pthread_mutex_lock(&subctx->self->process_row_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   206
        
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   207
        // tell it that it has a row waiting
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   208
        subctx->self->waiting_rows++;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   209
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   210
        // signal it in case it was waiting
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   211
        pthread_cond_signal(&subctx->self->process_row_cond);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   212
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   213
        // release the process_row mutex
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   214
        pthread_mutex_unlock(&subctx->self->process_row_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   215
    }
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   216
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   217
#define BEGIN_MUTEX(mut) pthread_cleanup_push((void (*)(void *)) pthread_mutex_unlock, &(mut)); pthread_mutex_lock(&(mut));
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   218
#define END_MUTEX pthread_cleanup_pop(1);
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   219
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   220
    // handle our can-continue status
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   221
    // we need to release this lock if we get canceled in pthread_cond_wait
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   222
    BEGIN_MUTEX(subctx->self->continue_mut)
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   223
        if (status & SLICE_CONTINUE) {
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   224
            // don't wait for anything, we can just continue right away
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   225
            DEBUG("slice %zu: direct continue", subctx->slice_info->index);
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   226
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   227
        } else {
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   228
            if (!subctx->can_continue) {
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   229
                // we need to wait until someone signals it
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   230
                DEBUG("slice %zu: waiting...", subctx->slice_info->index);
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   231
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   232
                pthread_cond_wait(&subctx->self->continue_cond, &subctx->self->continue_mut);
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   233
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   234
                DEBUG("slice %zu: continue after wait", subctx->slice_info->index);
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   235
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   236
            } else {
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   237
                // no need to wait, because someone else took care of that before we got a chance to wait
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   238
                DEBUG("slice %zu: indirect continue", subctx->slice_info->index);
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   239
             
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   240
            }
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   241
        }
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   242
        
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   243
        // we should now be marked as continueable
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   244
        assert(subctx->can_continue);
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   245
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   246
        // proceed to continue, so clear this
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   247
        subctx->can_continue = 0;
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   248
        
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   249
        // done handling the continue state
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   250
        pthread_mutex_unlock(&subctx->self->continue_mut);
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   251
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   252
    END_MUTEX
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   253
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   254
    // row handled succesfully
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   255
    return 0;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   256
}
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   257
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   258
void *_render_threads_slice_func (void *arg) {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   259
    struct render_thread *subctx = arg;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   260
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   261
    // set up the render_info to render into local memory using the address provided via slice_info
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   262
    // use _render_threads_slice_row to handle the completed rows
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   263
    if (render_local_mem(subctx->slice_info->render_info, 
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   264
        &subctx->slice_info->render_buf, 
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   265
        &_render_threads_slice_row, subctx)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   266
    )
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   267
        goto error;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   268
   
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   269
    // wait for the manager to start up
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   270
    pthread_mutex_lock(&subctx->self->continue_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   271
    if (!subctx->self->manager_running)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   272
        pthread_cond_wait(&subctx->self->continue_cond, &subctx->self->continue_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   273
    pthread_mutex_unlock(&subctx->self->continue_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   274
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   275
    // start rendering...
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   276
    DEBUG("slice %zu: start", subctx->slice_info->index);
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   277
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   278
    if (render_mandelbrot(subctx->slice_info->render_info))
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   279
        goto error;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   280
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   281
    // success, slice render complete
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   282
    DEBUG("slice %zu: done", subctx->slice_info->index);
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   283
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   284
    // sanity checks
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   285
    assert(subctx->self->slices_running > 0);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   286
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   287
    pthread_mutex_lock(&subctx->self->process_row_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   288
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   289
    if (--subctx->self->slices_running == 0) {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   290
        // this was the last row, send the manager a final signal in case it's waiting
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   291
        DEBUG("slice %zu: signal", subctx->slice_info->index);
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   292
        pthread_cond_signal(&subctx->self->process_row_cond);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   293
    }
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   294
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   295
    pthread_mutex_unlock(&subctx->self->process_row_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   296
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   297
    // successful exit
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   298
    pthread_exit(NULL);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   299
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   300
error:
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   301
    /* XXX: signal an error condition? Mind, with the current code, this should never happen... */
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   302
    FATAL("a render thread failed somehow, taking down the rest of the daemon application as well");
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   303
}
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   304
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   305
void *_render_threads_manager_func (void *arg) {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   306
    struct render_threads *ctx = arg;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   307
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   308
    // the process_row return status
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   309
    int status, i;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   310
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   311
    // first, mark us as running and signal any waiting workers
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   312
    pthread_mutex_lock(&ctx->continue_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   313
    ctx->manager_running = 1;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   314
    pthread_cond_broadcast(&ctx->continue_cond);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   315
    pthread_mutex_unlock(&ctx->continue_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   316
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   317
    // needs to be locked inside the loop
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   318
    pthread_mutex_lock(&ctx->process_row_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   319
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   320
    // then we wait around and call process_row when told to
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   321
    do {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   322
        // the process_row must be locked here
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   323
        DEBUG("manager: to wait");
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   324
        
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   325
        // figure out if we need to wait
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   326
        if (ctx->waiting_rows == 0 && ctx->slices_running > 0) {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   327
            // no work to do right now, so wait for some
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   328
            DEBUG("manager: waiting");
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   329
            
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   330
            // wait until a worker thread signals us
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   331
            pthread_cond_wait(&ctx->process_row_cond, &ctx->process_row_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   332
        }
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   333
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   334
        // make sure we actually have something to do now...
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   335
        assert(ctx->waiting_rows > 0 || ctx->slices_running == 0);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   336
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   337
        // keep the process_row mutex locked until we've inspected/updated both vars
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   338
        
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   339
        // do we need to process a row?
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   340
        if (ctx->waiting_rows > 0) {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   341
            // claim the row for ourself
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   342
            ctx->waiting_rows--;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   343
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   344
            // unlock the process_row mut while we handle this row
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   345
            pthread_mutex_unlock(&ctx->process_row_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   346
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   347
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   348
            // keep calls to render_slices synchronized
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   349
            pthread_mutex_lock(&ctx->slices_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   350
            
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   351
            // call into render_slices
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   352
            status = render_slices_process_row(&ctx->slices_info);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   353
            
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   354
            DEBUG("manager: did process_row, status=(%s %s)", 
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   355
                status & SLICE_PROCESS_ROW ? "SLICE_PROCESS_ROW" : "", 
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   356
                status & SLICE_CONTINUE ? "SLICE_CONTINUE" : ""
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   357
            );
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   358
           
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   359
            // any errors?
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   360
            if (status == -1) {
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   361
                pthread_mutex_unlock(&ctx->slices_mut);
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   362
                goto error;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   363
            }
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   364
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   365
            // can any waiting slices now continue?
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   366
            if (status & SLICE_CONTINUE) {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   367
                // grab the continue_mut lock while we update this state
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   368
                pthread_mutex_lock(&ctx->continue_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   369
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   370
                // set can_continue for all slices
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   371
                for (i = 0; i < ctx->slice_count; i++)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   372
                    ctx->threads[i].can_continue = 1;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   373
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   374
                DEBUG("manager: signal continue");
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   375
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   376
                // signal any waiting worker threads to continue, giving them a chance to call cond_wait first.
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   377
                pthread_cond_broadcast(&ctx->continue_cond);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   378
                
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   379
                // done with that
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   380
                pthread_mutex_unlock(&ctx->continue_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   381
            }
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   382
            
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   383
            // done with render_slices
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   384
            pthread_mutex_unlock(&ctx->slices_mut);
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   385
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   386
            // XXX: ignore SLICE_PROCESS_ROW
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   387
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   388
            // grab our mutex again
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   389
            pthread_mutex_lock(&ctx->process_row_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   390
        }
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   391
21
e2916f8ebaa6 fix memory alloc/free bugs, and one in render_threads where the last row was left out
Tero Marttila <terom@fixme.fi>
parents: 20
diff changeset
   392
        // are all the rows and slices done now?
e2916f8ebaa6 fix memory alloc/free bugs, and one in render_threads where the last row was left out
Tero Marttila <terom@fixme.fi>
parents: 20
diff changeset
   393
        if (ctx->slices_running == 0 && ctx->waiting_rows == 0) {
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   394
            // unlock the mutex so the worker threads don't get stuck on it
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   395
            pthread_mutex_unlock(&ctx->process_row_mut);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   396
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   397
            break;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   398
        }
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   399
        
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   400
        // keep the process_row mutex locked at the top of the loop 
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   401
    } while (1);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   402
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   403
    // all slices are done now
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   404
    if (render_slices_done(&ctx->slices_info))
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   405
        goto error;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   406
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   407
    DEBUG("manager: done");
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   408
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   409
    void *retval;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   410
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   411
    // reap all the workers
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   412
    for (i = 0; i < ctx->slice_count; i++) {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   413
        if (pthread_join(ctx->threads[i].thread, &retval))
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   414
            PERROR("pthread_join");
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   415
        
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   416
        // XXX: assume they don't get canceled
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   417
        assert(retval == NULL);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   418
    }
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   419
    
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   420
    // succesfull return
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   421
    ctx->manager_retval = RETVAL_SUCCESS;
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   422
    
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   423
    goto exit;
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   424
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   425
error:
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   426
    /* cancel and join all worker threads */
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   427
    WARNING("canceling and reaping all threads");
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   428
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   429
    for (i = 0; i < ctx->slice_count; i++) {
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   430
        if (pthread_cancel(ctx->threads[i].thread))
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   431
            PWARNING("pthread_cancel(worker:%d)", i);
21
e2916f8ebaa6 fix memory alloc/free bugs, and one in render_threads where the last row was left out
Tero Marttila <terom@fixme.fi>
parents: 20
diff changeset
   432
e2916f8ebaa6 fix memory alloc/free bugs, and one in render_threads where the last row was left out
Tero Marttila <terom@fixme.fi>
parents: 20
diff changeset
   433
        // join it in any case
e2916f8ebaa6 fix memory alloc/free bugs, and one in render_threads where the last row was left out
Tero Marttila <terom@fixme.fi>
parents: 20
diff changeset
   434
        if (pthread_join(ctx->threads[i].thread, &retval)) {
e2916f8ebaa6 fix memory alloc/free bugs, and one in render_threads where the last row was left out
Tero Marttila <terom@fixme.fi>
parents: 20
diff changeset
   435
            PWARNING("pthread_join(worker:%d)", i);
e2916f8ebaa6 fix memory alloc/free bugs, and one in render_threads where the last row was left out
Tero Marttila <terom@fixme.fi>
parents: 20
diff changeset
   436
        } else {
e2916f8ebaa6 fix memory alloc/free bugs, and one in render_threads where the last row was left out
Tero Marttila <terom@fixme.fi>
parents: 20
diff changeset
   437
            if (retval != PTHREAD_CANCELED)
e2916f8ebaa6 fix memory alloc/free bugs, and one in render_threads where the last row was left out
Tero Marttila <terom@fixme.fi>
parents: 20
diff changeset
   438
                PWARNING("worker:%d retval = %p", i, retval);
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   439
        }
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   440
    }
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   441
    
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   442
    // failure
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   443
    ctx->manager_retval = RETVAL_FAILURE;
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   444
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   445
exit:    
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   446
    // ok, exit ourself
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   447
    pthread_exit(ctx);
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   448
}
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   449
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   450
int render_threads_init (struct render_threads *ctx, struct render *render_info) {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   451
    // break the render operation down into induvidual slices
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   452
    if (render_slices_init(&ctx->slices_info, render_info))
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   453
        goto error;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   454
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   455
    // init the mutexes (this should never fail)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   456
    assert(!(
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   457
            pthread_mutex_init(&ctx->process_row_mut, NULL)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   458
         || pthread_mutex_init(&ctx->continue_mut, NULL)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   459
         || pthread_mutex_init(&ctx->slices_mut, NULL)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   460
    ));
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   461
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   462
    // init the conds (this should never fail)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   463
    assert(!(
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   464
            pthread_cond_init(&ctx->process_row_cond, NULL)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   465
         || pthread_cond_init(&ctx->continue_cond, NULL)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   466
    ));
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   467
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   468
    // how many slices?
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   469
    ctx->slices_running = ctx->slice_count = render_slices_get_count(&ctx->slices_info);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   470
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   471
    // spawn a thread for each slice
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   472
    int index;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   473
    for (index = 0; index < ctx->slice_count; index++) {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   474
        // the self ptr...
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   475
        ctx->threads[index].self = ctx;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   476
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   477
        // fetch the slice info
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   478
        ctx->threads[index].slice_info = render_slices_get_slice_info(&ctx->slices_info, index);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   479
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   480
        // spawn the thread
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   481
        if (pthread_create(&ctx->threads[index].thread, NULL, &_render_threads_slice_func, &ctx->threads[index]))
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   482
            PERROR("pthread_create(slice_func:%d)", index);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   483
    }
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   484
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   485
    // init the attrs for the manager thread
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   486
    pthread_attr_t manager_attrs;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   487
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   488
    if (
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   489
            pthread_attr_init(&manager_attrs)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   490
//         || pthread_setdetachstate(&manager_attrs, PTHREAD_CREATE_DETACHED)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   491
    )
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   492
        PERROR("pthread_attr_init/pthread_attr_setdetachstate");
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   493
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   494
    // spawn the manager thread
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   495
    if (pthread_create(&ctx->manager_thread, &manager_attrs, &_render_threads_manager_func, ctx))
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   496
        PERROR("pthread_create(manager_func)");
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   497
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   498
    // success
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   499
    return 0;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   500
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   501
error:
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   502
    render_threads_deinit(ctx);
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   503
    return -1;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   504
}
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   505
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   506
struct render_threads *render_threads_alloc (struct render *render_info) {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   507
    struct render_threads *ctx = NULL;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   508
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   509
    if (!(ctx = calloc(1, sizeof(*ctx))))
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   510
        ERROR("calloc");
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   511
    
21
e2916f8ebaa6 fix memory alloc/free bugs, and one in render_threads where the last row was left out
Tero Marttila <terom@fixme.fi>
parents: 20
diff changeset
   512
    // flag it for free()ing
e2916f8ebaa6 fix memory alloc/free bugs, and one in render_threads where the last row was left out
Tero Marttila <terom@fixme.fi>
parents: 20
diff changeset
   513
    ctx->owned_by_me = 1;
e2916f8ebaa6 fix memory alloc/free bugs, and one in render_threads where the last row was left out
Tero Marttila <terom@fixme.fi>
parents: 20
diff changeset
   514
    
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   515
    // init with silent fall-through
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   516
    if (render_threads_init(ctx, render_info))
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   517
        goto error;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   518
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   519
    // success
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   520
    return ctx;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   521
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   522
error:
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   523
    return NULL;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   524
}
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   525
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   526
int render_threads_wait (struct render_threads *ctx) {
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   527
    void *retval;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   528
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   529
    if (pthread_join(ctx->manager_thread, &retval))
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   530
        PERROR("pthread_join");
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   531
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   532
    if (retval == PTHREAD_CANCELED)
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   533
        ERROR("manager thread was canceled");
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   534
    else if (retval != ctx)
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   535
        ERROR("manager thread exited with unknown return value");
20
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   536
    else {
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   537
        if (ctx->manager_retval != RETVAL_SUCCESS) {
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   538
            ERROR("manger thread exited with an error");
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   539
        }
7512207c9041 write a DEBUG macro and change the render_threads stuff to use it. Shuffle the mutexes in render_threads around again to fix yet another bug... seems to work, but meh
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
   540
    }
19
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   541
    
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   542
    // ok, success
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   543
    return 0;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   544
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   545
error:
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   546
    // caller needs to free ctx
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   547
    return -1;
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   548
}
d18606bb6f20 a working threaded sliced render, plus modifications to other modules to use this in web_main
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   549