r1006: configure: Use libx264_pic instead of libx264 if available.
[cinelerra_cv/mob.git] / cinelerra / threadfork.C
blob2f7342505523a1ef383d5ac2d9efbfd392dff10d
1 #include "threadfork.h"
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <sys/wait.h>
7 #include <unistd.h>
9 #define MAX_ARGS 32
14 ThreadFork::ThreadFork()
16         pid = 0;
17         tid = 0;
18         pthread_mutexattr_t attr;
19         pthread_mutexattr_init(&attr);
20         pthread_mutex_init(&start_lock, &attr);
24         arguments = new char*[MAX_ARGS];
25         total_arguments = 0;
26         stdin_fd = 0;
27         pipe_stdin = 0;
28         command_line = "";
31 ThreadFork::~ThreadFork()
33 //      if(pipe_stdin)
34 //      {
35                 pclose(stdin_fd);
36 //              if(stdin_fd) fclose(stdin_fd);
37 //              close(filedes[0]);
38 //              close(filedes[1]);
39 //      }
41 //      pthread_join(tid, 0);
43         for(int i = 0; i < total_arguments; i++)
44                 delete [] arguments[i];
46         pthread_mutex_destroy(&start_lock);
47         delete [] arguments;
50 void* ThreadFork::entrypoint(void *ptr)
52         ThreadFork *threadfork = (ThreadFork*)ptr;
53         threadfork->run();
56 void ThreadFork::start_command(char *command_line, int pipe_stdin)
58         this->command_line = command_line;
59         this->pipe_stdin = pipe_stdin;
61         pthread_attr_t  attr;
62         pthread_attr_init(&attr);
65         stdin_fd = popen(command_line, "w");
66         
67 //      pthread_mutex_lock(&start_lock);
70 //      pthread_create(&tid, &attr, entrypoint, this);
71 //      pthread_mutex_lock(&start_lock);
72 //      pthread_mutex_unlock(&start_lock);
76 void ThreadFork::run()
78         char *path_ptr;
79         char *ptr;
80         char *argument_ptr;
81         char argument[BCTEXTLEN];
82         char command_line[BCTEXTLEN];
83         int new_pid;
85         strcpy(command_line, this->command_line);
89 // Set up arguments for exec
90         ptr = command_line;
91         path_ptr = path;
92         while(*ptr != ' ' && *ptr != 0)
93         {
94                 *path_ptr++ = *ptr++;
95         }
96         *path_ptr = 0;
98         arguments[total_arguments] = new char[strlen(path) + 1];
99         strcpy(arguments[total_arguments], path);
101         total_arguments++;
102         arguments[total_arguments] = 0;
104         while(*ptr != 0)
105         {
106                 ptr++;
107                 argument_ptr = argument;
108                 while(*ptr != ' ' && *ptr != 0)
109                 {
110                         *argument_ptr++ = *ptr++;
111                 }
112                 *argument_ptr = 0;
114                 arguments[total_arguments] = new char[strlen(argument) + 1];
115                 strcpy(arguments[total_arguments], argument);
116                 total_arguments++;
117                 arguments[total_arguments] = 0;
118         }
129         if(pipe_stdin)
130         {
131                 pipe(filedes);
132                 stdin_fd = fdopen(filedes[1], "w");
133         }
135         pthread_mutex_unlock(&start_lock);
139         new_pid = vfork();
141 // Redirect stdin
142         if(new_pid == 0)
143         {
144                 if(pipe_stdin) dup2(filedes[0], fileno(stdin));
148                 execvp(path, arguments);
152                 perror("execvp");
153                 _exit(0);
154         }
155         else
156         {
157                 pid = new_pid;
158                 pthread_mutex_unlock(&start_lock);
159                 
160                 int return_value;
161                 if(waitpid(pid, &return_value, WUNTRACED) == -1)
162                 {
163                         perror("ThreadFork::run: waitpid");
164                 }
165         }
171 FILE* ThreadFork::get_stdin()
173         return stdin_fd;