task_meta.h 3.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

gejun's avatar
gejun committed
18 19 20 21 22
// bthread - A M:N threading library to make applications more concurrent.

// Author: Ge,Jun (gejun@baidu.com)
// Date: Tue Jul 10 17:40:58 CST 2012

gejun's avatar
gejun committed
23 24
#ifndef BTHREAD_TASK_META_H
#define BTHREAD_TASK_META_H
gejun's avatar
gejun committed
25 26 27

#include <pthread.h>                 // pthread_spin_init
#include "bthread/butex.h"           // butex_construct/destruct
28
#include "butil/atomicops.h"          // butil::atomic
gejun's avatar
gejun committed
29
#include "bthread/types.h"           // bthread_attr_t
gejun's avatar
gejun committed
30
#include "bthread/stack.h"           // ContextualStack
gejun's avatar
gejun committed
31 32 33 34 35 36 37 38

namespace bthread {

struct TaskStatistics {
    int64_t cputime_ns;
    int64_t nswitch;
};

39
class KeyTable;
gejun's avatar
gejun committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53
struct ButexWaiter;

struct LocalStorage {
    KeyTable* keytable;
    void* assigned_data;
    void* rpcz_parent_span;
};

#define BTHREAD_LOCAL_STORAGE_INITIALIZER { NULL, NULL, NULL }

const static LocalStorage LOCAL_STORAGE_INIT = BTHREAD_LOCAL_STORAGE_INITIALIZER;

struct TaskMeta {
    // [Not Reset]
54
    butil::atomic<ButexWaiter*> current_waiter;
gejun's avatar
gejun committed
55
    uint64_t current_sleep;
56 57

    // A builtin flag to mark if the thread is stopping.
gejun's avatar
gejun committed
58
    bool stop;
59 60 61 62 63

    // The thread is interrupted and should wake up from some blocking ops.
    bool interrupted;

    // Scheduling of the thread can be delayed.
gejun's avatar
gejun committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    bool about_to_quit;
    
    // [Not Reset] guarantee visibility of version_butex.
    pthread_spinlock_t version_lock;
    
    // [Not Reset] only modified by one bthread at any time, no need to be atomic
    uint32_t* version_butex;

    // The identifier. It does not have to be here, however many code is
    // simplified if they can get tid from TaskMeta.
    bthread_t tid;

    // User function and argument
    void* (*fn)(void*);
    void* arg;

gejun's avatar
gejun committed
80 81
    // Stack of this task.
    ContextualStack* stack;
gejun's avatar
gejun committed
82 83 84 85 86 87 88 89

    // Attributes creating this task
    bthread_attr_t attr;
    
    // Statistics
    int64_t cpuwide_start_ns;
    TaskStatistics stat;

90 91 92
    // bthread local storage, sync with tls_bls (defined in task_group.cpp)
    // when the bthread is created or destroyed.
    // DO NOT use this field directly, use tls_bls instead.
gejun's avatar
gejun committed
93 94 95 96 97 98 99 100
    LocalStorage local_storage;

public:
    // Only initialize [Not Reset] fields, other fields will be reset in
    // bthread_start* functions
    TaskMeta()
        : current_waiter(NULL)
        , current_sleep(0)
gejun's avatar
gejun committed
101
        , stack(NULL) {
gejun's avatar
gejun committed
102
        pthread_spin_init(&version_lock, 0);
gejun's avatar
gejun committed
103
        version_butex = butex_create_checked<uint32_t>();
gejun's avatar
gejun committed
104 105 106 107
        *version_butex = 1;
    }
        
    ~TaskMeta() {
gejun's avatar
gejun committed
108
        butex_destroy(version_butex);
gejun's avatar
gejun committed
109 110 111 112
        version_butex = NULL;
        pthread_spin_destroy(&version_lock);
    }

gejun's avatar
gejun committed
113 114
    void set_stack(ContextualStack* s) {
        stack = s;
gejun's avatar
gejun committed
115 116
    }

gejun's avatar
gejun committed
117 118 119
    ContextualStack* release_stack() {
        ContextualStack* tmp = stack;
        stack = NULL;
gejun's avatar
gejun committed
120 121 122 123 124 125 126 127 128 129
        return tmp;
    }

    StackType stack_type() const {
        return static_cast<StackType>(attr.stack_type);
    }
};

}  // namespace bthread

gejun's avatar
gejun committed
130
#endif  // BTHREAD_TASK_META_H