<commit_before>/*
 * Copyright 2008, Yahoo! Inc.
 *
 * Licensed 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.
 */
#ifndef THREADED
#define THREADED
#endif

#define BUILD_LIB

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include "zk_adaptor.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <signal.h>
#include <sys/select.h>


void lock_buffer_list(buffer_head_t *l)
{
    pthread_mutex_lock(&l->lock);
}
void unlock_buffer_list(buffer_head_t *l)
{
    pthread_mutex_unlock(&l->lock);
}
void lock_completion_list(completion_head_t *l)
{
    pthread_mutex_lock(&l->lock);
}
void unlock_completion_list(completion_head_t *l)
{
    pthread_cond_broadcast(&l->cond);
    pthread_mutex_unlock(&l->lock);
}
struct sync_completion *alloc_sync_completion(void)
{
    struct sync_completion *sc = (struct sync_completion*)calloc(1, sizeof(struct sync_completion));
    if (sc) {
       pthread_cond_init(&sc->cond, 0); 
       pthread_mutex_init(&sc->lock, 0);
    }
    return sc;
}
int wait_sync_completion(struct sync_completion *sc)
{
    pthread_mutex_lock(&sc->lock);
    while (!sc->complete) {
        pthread_cond_wait(&sc->cond, &sc->lock);
    }
    pthread_mutex_unlock(&sc->lock);
    return 0;
}

void free_sync_completion(struct sync_completion *sc)
{
    if (sc) {
        pthread_mutex_destroy(&sc->lock);
        pthread_cond_destroy(&sc->cond);
        free(sc);
    }
}

void notify_sync_completion(struct sync_completion *sc)
{
    pthread_mutex_lock(&sc->lock);
    sc->complete = 1;
    pthread_cond_broadcast(&sc->cond);
    pthread_mutex_unlock(&sc->lock);
}

int process_async(int outstanding_sync)
{
    return 0;
}
struct adaptor_threads {
    pthread_t io;
    pthread_t completion;
};

void *do_io(void *);
void *do_completion(void *);

int adaptor_init(zhandle_t *zh)
{
    struct adaptor_threads *adaptor_threads = calloc(1, sizeof(*adaptor_threads)); 
    if (!adaptor_threads) {
        return -1;
    }
    zh->adaptor_priv = adaptor_threads;
    api_prolog(zh);
    pthread_create(&adaptor_threads->io, 0, do_io, zh);
    api_prolog(zh);
    pthread_create(&adaptor_threads->completion, 0, do_completion, zh);
    return 0;
}

void adaptor_finish(zhandle_t *zh)
{
    struct adaptor_threads *adaptor_threads = zh->adaptor_priv;
      
    if (zh->state >= 0) {
        fprintf(stderr, "Forcibly setting state to CLOSED\n");
        zh->state = -1;
    }
    pthread_kill(adaptor_threads->io, SIGUSR1);
    pthread_mutex_lock(&zh->completions_to_process.lock);
    pthread_cond_broadcast(&zh->completions_to_process.cond);
    pthread_mutex_unlock(&zh->completions_to_process.lock);
    pthread_join(adaptor_threads->io, 0);
    pthread_join(adaptor_threads->completion, 0);
}

int adaptor_send_queue(zhandle_t *zh, int timeout)
{
    struct adaptor_threads *adaptor_threads = zh->adaptor_priv;
    pthread_kill(adaptor_threads->io, SIGUSR1);
    return 0;
}
<commit_msg>Make sure mutexes and condition variables are properly initialized and destroyed<commit_after>/*
 * Copyright 2008, Yahoo! Inc.
 *
 * Licensed 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.
 */
#ifndef THREADED
#define THREADED
#endif

#define BUILD_LIB

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include "zk_adaptor.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <signal.h>
#include <sys/select.h>


void lock_buffer_list(buffer_head_t *l)
{
    pthread_mutex_lock(&l->lock);
}
void unlock_buffer_list(buffer_head_t *l)
{
    pthread_mutex_unlock(&l->lock);
}
void lock_completion_list(completion_head_t *l)
{
    pthread_mutex_lock(&l->lock);
}
void unlock_completion_list(completion_head_t *l)
{
    pthread_cond_broadcast(&l->cond);
    pthread_mutex_unlock(&l->lock);
}
struct sync_completion *alloc_sync_completion(void)
{
    struct sync_completion *sc = (struct sync_completion*)calloc(1, sizeof(struct sync_completion));
    if (sc) {
       pthread_cond_init(&sc->cond, 0); 
       pthread_mutex_init(&sc->lock, 0);
    }
    return sc;
}
int wait_sync_completion(struct sync_completion *sc)
{
    pthread_mutex_lock(&sc->lock);
    while (!sc->complete) {
        pthread_cond_wait(&sc->cond, &sc->lock);
    }
    pthread_mutex_unlock(&sc->lock);
    return 0;
}

void free_sync_completion(struct sync_completion *sc)
{
    if (sc) {
        pthread_mutex_destroy(&sc->lock);
        pthread_cond_destroy(&sc->cond);
        free(sc);
    }
}

void notify_sync_completion(struct sync_completion *sc)
{
    pthread_mutex_lock(&sc->lock);
    sc->complete = 1;
    pthread_cond_broadcast(&sc->cond);
    pthread_mutex_unlock(&sc->lock);
}

int process_async(int outstanding_sync)
{
    return 0;
}
struct adaptor_threads {
    pthread_t io;
    pthread_t completion;
};

void *do_io(void *);
void *do_completion(void *);

int adaptor_init(zhandle_t *zh)
{
    struct adaptor_threads *adaptor_threads = calloc(1, sizeof(*adaptor_threads)); 
    if (!adaptor_threads) {
        return -1;
    }
    zh->adaptor_priv = adaptor_threads;
    pthread_mutex_init(&zh->to_process.lock,0);
    pthread_mutex_init(&zh->to_send.lock,0);
    pthread_mutex_init(&zh->sent_requests.lock,0);
    pthread_cond_init(&zh->sent_requests.cond,0);
    pthread_mutex_init(&zh->completions_to_process.lock,0);
    pthread_cond_init(&zh->completions_to_process.cond,0);
    api_prolog(zh);
    pthread_create(&adaptor_threads->io, 0, do_io, zh);
    api_prolog(zh);
    pthread_create(&adaptor_threads->completion, 0, do_completion, zh);
    return 0;
}

void adaptor_finish(zhandle_t *zh)
{
    struct adaptor_threads *adaptor_threads = zh->adaptor_priv;
      
    if (zh->state >= 0) {
        fprintf(stderr, "Forcibly setting state to CLOSED\n");
        zh->state = -1;
    }
    pthread_kill(adaptor_threads->io, SIGUSR1);
    pthread_mutex_lock(&zh->completions_to_process.lock);
    pthread_cond_broadcast(&zh->completions_to_process.cond);
    pthread_mutex_unlock(&zh->completions_to_process.lock);
    pthread_join(adaptor_threads->io, 0);
    pthread_join(adaptor_threads->completion, 0);
    pthread_mutex_destroy(&zh->to_process.lock);
    pthread_mutex_destroy(&zh->to_send.lock);
    pthread_mutex_destroy(&zh->sent_requests.lock);
    pthread_cond_destroy(&zh->sent_requests.cond);
    pthread_mutex_destroy(&zh->completions_to_process.lock);
    pthread_cond_destroy(&zh->completions_to_process.cond);
}

int adaptor_send_queue(zhandle_t *zh, int timeout)
{
    struct adaptor_threads *adaptor_threads = zh->adaptor_priv;
    pthread_kill(adaptor_threads->io, SIGUSR1);
    return 0;
}

{'fraction_non_alphanumeric': 0.12062821697241652, 'fraction_numerical': 0.007654744621882011, 'mean_word_length': 4.340380549682875, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4095102', 'n_tokens_mistral': 2741, 'n_tokens_neox': 2600, 'n_words': 532}